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:
When user.id is 42, the rendered HTML is:
String values need quotes
Strings must be quoted in the generated JavaScript:
That produces:
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:
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:
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.
Related reading
- UTF-8 byte[] to String
- UTF-8 encoding of application.properties attributes in Spring-Boot
- Validation failed for query for method JPQL
- Value Change Listener to JTextField
- Value Could not resolve placeholder in Spring Boot Test
- Verify object attribute value with mockito
- Very slow Spring Boot application startup
- View's getWidth() and getHeight() returns 0
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.