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
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>
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
Matthias Steinbauer
Good to hear that! Please mark the correct answer as the accepted answer then. Best Matthias!