I need to call a javascript function on thymeleaf template, something like this:
Case 1:
<select th:onclick="${'function1('a')'}">
But in this case the thymeleaf not work.. some researchs ago (including stackoverflow) I get the followings "solutions":
Case 2:
<select th:onclick="${'function1(''a'')'}">
Case 3:
<select th:onclick="${'function1(\'a\')'}">
Case 4:
<select th:onclick="${'function1(\''+'a'+'\')'}">
But in all cases I get the same error: "...Exception evaluating SpringEL expression..."
My problem is about javascript callings, I need put some parameters ${var} for call in js function. How I can fix that ?
Thanks
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.