Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using thymeleaf variable in onclick attribute

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.

like image 544
Kleber Mota Avatar asked Sep 18 '15 11:09

Kleber Mota


People also ask

How do I use onclick in Thymeleaf?

10, thymeleaf doesn't support variables of type other than Boolean and Integer inside th:onclick="..." . So if you want to pass a non-boolean and non-integer argument to the onclick() method, th:onclick="..." won't work. You have to use th:attr="onclick=..." as shown in this answer.

How do you set a variable in Thymeleaf?

We can use the th:with attribute to declare local variables in Thymeleaf templates. A local variable in Thymeleaf is only available for evaluation on all children inside the bounds of the HTML tag that declares it.

Can I use Thymeleaf in JavaScript?

JavaScript inlining is a powerful feature that allows us to dynamically set the values of JavaScript variables using data from Spring MVC Model object in Thymeleaf. JavaScript natural templating is another way of using JavaScript inlining while quickly crafting a static prototype of the website.

How do you use Thymeleaf?

You can use Thymeleaf templates to create a web application in Spring Boot. You will have to follow the below steps to create a web application in Spring Boot by using Thymeleaf. In the above example, the request URI is /index, and the control is redirected into the index. html file.


1 Answers

thymeleaf 3.0.10 th:onclick thymeleaf variable not working

This should work:

th:attr="onclick=|upload('${gallery}')|"  
like image 75
LiaNg Avatar answered Oct 14 '22 13:10

LiaNg