How to prevent single quote in th:onclick getting escaped when using thymeleaf

The name of the pictureThe name of the pictureThe name of the pictureClash 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.





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 shows onclick="alert('null');"
– MasterQiao
Aug 6 at 10:00



th:onclick="|alert('$myVar');|"


onclick="alert('null');"





Shows &#39: 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.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS