0

enter image description here

I send values using model.addAttribute in Spring, so I get values in HTML file well. and I want to get values in js files so I use document.getElementById but I can't get the value but 0 .. How to get html thymeleaf value in js? Thank you all..

1 Answer 1

2

This you find this documented here: https://www.concretepage.com/thymeleaf/thymeleaf-javascript-inline-example-with-variable

The part which is interesting to you is the following:

<script th:inline="javascript">

/*<![CDATA[*/
  var user = /*[[${session.userName}]]*/ 'User Name';

/*]]>*/
</script>

In your case of course instead of session.userName you would put your variable name.

<script th:inline="javascript">

/*<![CDATA[*/
  var user = /*[[${modelVariable}]]*/ 'User Name';

/*]]>*/
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

OMG,,, I appreciate your kindness.. I'm really tired of this problem, but I solve it with your solution. Thanks to you, I can skip to next level. I wish you have a nice day.. Thank you
Good to hear that! Please mark the correct answer as the accepted answer then. Best Matthias!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.