How to prevent single quote in th:onclick getting escaped when using thymeleaf
Clash Royale CLAN TAG#URR8PPP
How to prevent single quote in th:onclick getting escaped when using thymeleaf
I want to get onclick="alert('myvar')"
in the browser.
onclick="alert('myvar')"
I tried
th:onclick="'alert('' + $myVar + '');'"
and I got
onclick="alert('null');"
I tried
th:onclick="|alert('$myVar');|"
and I still got
onclick="alert('null');"
How can I let the single quote not be escaped?
1 Answer
1
The following snipped worked for me:
th:onclick="|alert('$myVar');|"
That looks like your second attemp but I'm getting:
onclick="alert('null');"
That makes me thinking and I tested your first attemp:
th:onclick="'alert('' + $myVar + '');'"
and again I'm getting:
onclick="alert('null');"
Spooky! I searched for similar questions in the thymleaf-forum afterwards and that's the way how to to this.
th:onclick="|alert('$myVar');|"
onclick="alert('null');"
Shows ': but that isn't a problem since ' is the decimal name of the entity '. The editor-like window on the right side just shows you the entities decimal name.
– Flocke
Aug 6 at 12:21
Yes, it works rightly just like what I need. I thought
'
in onclick
would be a syntax error before. Thank you very much.– MasterQiao
Aug 7 at 2:00
'
onclick
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
So that's maybe a question related with configuration or version of thymeleaf. Would you help to try itutorial.thymeleaf.org/exercise/12 , add
th:onclick="|alert('$myVar');|"
to the submit button and see if it showsonclick="alert('null');"
– MasterQiao
Aug 6 at 10:00