How to set the id attribute of HTML element dynamically with Thymeleaf

Lazar Lazarov picture Lazar Lazarov · Mar 1, 2016 · Viewed 35k times · Source

Lets say I have an object : ${object}

and I have the following form:

<form id="{{'myForm' + object.id}" class="some class"
      th:action="@{/doSomething}" method="post">
    ....
</form>

My goal is to set the id = "myForm1" if we assume that the object.id is '1'.

PS: The way I wrote it's working on Angular JS.

Answer

Sergio Garcia Alonso picture Sergio Garcia Alonso · Mar 1, 2016

You have to use th:id attribute:

<form th:id="'myForm' + ${object.id}" class="some class" th:action="@{/doSomething}" method="post">
// *** Other code here ***
</form>