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.
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.
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.
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.
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.
thymeleaf 3.0.10 th:onclick thymeleaf variable not working
This should work:
th:attr="onclick=|upload('${gallery}')|"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With