Thymeleaf
onclick attribute
Java
web development
template engine

Using a Thymeleaf variable in an onclick attribute

Codemia Knowledge Hub ·

Build the onclick value with th:attr and Thymeleaf's literal substitution (the |...| syntax), for example th:attr="onclick=|showUser(${user.id})|". Numbers can go straight into the call, but string values need JavaScript quotes inside the generated value. For anything beyond a tiny call, put the value in a data-* attribute and attach the handler in JavaScript instead.

Numeric values

A numeric value can be inserted directly into the function call:

html
1<button
2  th:attr="onclick=|showUser(${user.id})|">
3  View user
4</button>

When user.id is 42, the rendered HTML is:

html
<button onclick="showUser(42)">View user</button>

String values need quotes

Strings must be quoted in the generated JavaScript:

html
1<button
2  th:attr="onclick=|showUserName('${user.name}')|">
3  View name
4</button>

That produces:

html
<button onclick="showUserName('Alice')">View name</button>

The quotes JavaScript needs are part of the generated attribute value, so they do not depend on things lining up by accident. Broken quoting is the most common bug here: a template looks right in the editor but renders invalid JavaScript because a string value was not wrapped.

Why literal substitution helps

The pipe-delimited syntax keeps mixed values readable:

html
th:attr="onclick=|deleteItem(${item.id}, '${item.code}')|"

String concatenation gets noisy and fragile quickly, so literal substitution is usually the clearest option for one small JavaScript call with server-side values in it.

Better: data-* attributes and an event listener

Inline onclick handlers mix behavior with markup. A cleaner pattern renders the data into data-* attributes and lets external JavaScript attach the click behavior:

html
1<button
2  class="delete-button"
3  th:attr="data-user-id=${user.id}">
4  Delete
5</button>
javascript
1document.querySelectorAll(".delete-button").forEach((button) => {
2  button.addEventListener("click", () => {
3    const userId = button.dataset.userId;
4    deleteUser(userId);
5  });
6});

This is easier to test and maintain, and less error-prone once the call grows beyond a one-liner. It is also safer: user-controlled values placed carelessly inside inline JavaScript can create injection problems, while keeping values in data-* attributes and behavior in separate scripts avoids that.

If you already use Thymeleaf JavaScript inlining in a script block, another clean option is to keep the HTML simple and use the Thymeleaf values in the script, rather than building large inline onclick strings. As a rule:

  • small one-off event call: an inline attribute can be fine
  • richer client-side behavior: prefer data-* plus event listeners

Debug the rendered HTML

When an onclick expression misbehaves, inspect the final HTML in the browser, not just the template source. What runs is the rendered attribute value, and broken quotes or malformed JavaScript are usually obvious there. Packing too much logic into the attribute makes this debugging painful and escaping easy to get wrong.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms