Javascript function call with Thymeleaf

If you don't need any dynamic vars in the JS function call, this is how to do it:

th:onclick="'alert(\'a\');'"

This simply escapes the single quotes and requires no SpringEL (of course you could dispense with the thymeleaf attribute in this case and just use plain onclick).

To insert vars into it:

th:onclick="'alert(\'' + ${myVar} + '\');'"

Used the alert function to allow me to try it out and prove it works. Hope that helps.


You need to call the javascript function as mentioned below.

th:onclick="'javascript:function1(\''+ ${a} +'\');'"

I think this could help you.