Using thymeleaf variable in onclick attribute

Issue

In my current spring-boot project, I have one view with this html code:

<button type="button" class="btn btn-primary" onclick="upload()" th:utext="#{modal.save}"></button>

in the onclick attribute, the call for the function upload() should have one parameter, which value is stored in the thymeleaf variable ${gallery}.

Anyone can tell mehow to use the expression in the above command?

I already try this:

  • th:onclick="upload(${gallery)"

  • th:attr="onclick=upload(${gallery)"

None of this worked.

Solution

I solve this issue with this approach:

th:onclick="|upload('${command['class'].simpleName}', '${gallery}')|"

Answered By – Kleber Mota

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published