Using thymeleaf variable in onclick attribute

Kleber Mota picture Kleber Mota · Sep 18, 2015 · Viewed 53.2k times · Source

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.

Answer

Kleber Mota picture Kleber Mota · Sep 24, 2015

I solve this issue with this approach:

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