44

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.

1

8 Answers 8

64

thymeleaf 3.0.10 th:onclick thymeleaf variable not working

This should work:

th:attr="onclick=|upload('${gallery}')|" 
Sign up to request clarification or add additional context in comments.

2 Comments

Really? You can find th:onclick in the docs for thymleaf 3.0. I'm pretty shure the attribute isn't kicked out for version 3.0.10
This is the correct answer because since version 3.0.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.
43

I solve this issue with this approach:

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

2 Comments

At now (2020) it seems does not work anymore: Error: Only variable expressions returning numbers or booleans are allowed in this context, any other datatypes are not trusted
At now the answer here its working: stackoverflow.com/a/52960571/373498
14

This should work:

<button th:onclick="'javascript:upload(' + ${gallery} + ')'"></button>

3 Comments

This works perfectly but you've left out the closing brace '}'. Should be <button th:onclick="'javascript:upload(' + ${gallery} + ')'"></button>. Also you might need to enclose gallery in quotes if it is a String parameter, so <button th:onclick="'javascript:upload('\' + ${gallery} + '\')'"></button>.
@phn Added the missing brace. Thanks!
Thanks @cnpfm this answer is more suitable for my problem in Thymeleaf 2
12

In 3.0.10 version of Thymeleaf

use [[ ]] it's more easier , thymeleaf goes to inject the value in template

if the value is string no quote need

<button th:data-id="${quartzInfo.id}" 
    th:onclick="del(this,[[${quartzInfo.id}]]" type="button">
</button>

2 Comments

This worked for me!!
This is missing the closing parentheses for the 'del' method
6

The older replies does not work in the new version of 3.0.10. Now you need:

<button th:data-id="${quartzInfo.id}" 
    onclick="del(this,this.getAttribute('data-id'))" type="button">
</button>

Comments

3

In fact this one is working too :

th:attr="onclick=${'upload('+gallery+')'}"

1 Comment

th:attr="onclick=${'upload('''+gallery+''')'}" make it like so to wrap single quotes around the object within bracket
2

yes it's to late, but i hope this will help people who need it

try this

th:onclick="'upload('+${gallery}+')'"

Comments

0

If you are going to use attr, where there is a string the HTML will crash and you have to do the following, in case of string values.

th:attr="onclick=${'toggleContractLine('+ ''' + contract.id + ''' + ')'}"

1 Comment

It might be one more of those ' this worked for me <span th:attr="onclick=${'doThis('+ '''' + user.getTestJquery() + '''' + ')'}">aaaa</span>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.