Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

java - How to pass arguments to javascript function call when using onClick in Thymeleaf

I have been calling my javascript function from Thymeleaf as below:

th:onclick="'viewDocument('' + ${document.docTypeLongDesc} +'');'"

But I just updated my spring boot version to 2.1.4 RELEASE with which Thymeleaf also got updated. And the previous version in no longer supported.

On further research I found out that I should be able to use

th:onclick="' viewDocument (this.getAttribute ('document.docTypeLongDesc'));'"

However, it doesn't give any error but neither does it work. I have removed the argument and was able to call the function just fine. So I am guessing I am not passing the argument right way. Any guidance will be helpful. TIA.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

See this: Restricted mode: Avoid variable expressions returning strings in processors for event handlers (th:on*).

In order to correctly pass Thymeleaf variables to the onclick event, put the variable in a data attribute, and read it using getAttribute().

th:data-longDescription="${document.docTypeLongDesc}" onclick="viewDocument(this.getAttribute('data-longDescription'));"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...