11

I searched first but I found confusing answers since I'm new to Thymeleaf and amateurish at best at javascript.

I just want to know how to pass variable expressions into javascript functions, sort of like in JSP:

<a href="#" onclick="javascript:getContactId('${contact.id}');">Button</a>

Of course, this fails with Thymeleaf and passes the string ${contact.id} instead of its value, so how could I get the value of the variable expression instead?

The reason I want it this way is because it depends on the row which is being iterated by th:each.

If there's no other way except to use th:inline, then what's the best approach considering the above statement?

6 Answers 6

29

This one worked:

th:onclick="'javascript:getContactId(\'' + ${contact.id} + '\');'"

Thanks goes out to the thymeleaf forum: http://forum.thymeleaf.org/variable-expression-into-javascript-without-using-th-inline-td4025534.html

Sign up to request clarification or add additional context in comments.

2 Comments

This works for me, with one parameter, but I can't (yet) see how that looks like with two parameters?
Ah nevermind, got it! For (example) a fixed string param, the above will be th:onclick="'javascript:getContactId(\'someString\',\'' + ${contact.id} + '\');'" Cool, thanks!
6

In Thymeleaf version 2.1, I have not been able to get the accepted answer to work. I did find some guidance from a Twitter post from Thymeleaf. Rather than using th:onclick, you use th:attr and specify the onclick attribute therein.

th:attr="onclick='javascript:getContactId(\'' + ${contact.id} + '\');'"

1 Comment

Thank you thank you thank you! This is the only thing that worked for me. It is a bit annoying that the answer had to be found in a cryptic tweet! :-)
0

You can not put javascript variables into onclick or other DOM attributes. The value of onclick or any other DOM attribute should be a constant string.

However, you can dynamically modify value of onclick attribute from javascript, like this:

yourDomElement.onclick = anyVariable;

9 Comments

what do you mean? this would be part of an <a> tag.
the part of <a> tag should be a constant string. Dynamically you can do that from inside <script language="javascript"> block
do you mean that I should not? Because this has worked for me in the past.
I'm calling an external function by its name, the whole function isn't there. Anyway this isn't part of my problem..
once agagin, value passed to onclick inside <a> element should be a constant string. You can not pass javascript variables there. To do so you should set value of yourDomElement.onclick from javascript.
|
0

You can do this like:

th:onclick="'javascript:getContactId(\'' + ${contact.id} + '\');'"

Comments

0

A more generic approach, if you need in JS something that isn't passed as a event handler parameter:

th:attr="data-myValueFromThymeleaf=${contact.id}"

Any attribute whose name is starting with data- is ignored by all browsers. So it won't affect the UI and you can easily read the value in javascript.

I prefer this because it's not ideal to put javascript code in html (see unobtrusive javascript)

Comments

0

I have asked the Thymeleaf project on twitter, and their answer is:

You can use both the "+" operator or literal substitutions. For example: <a th:href="|javascript:change('start','${taskId}')|">

For more info: https://www.thymeleaf.org/doc/articles/standarddialect5minutes.html

I have tried, and it works~

Comments

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.