Search code examples
spring-bootthymeleaf

Thymeleaf onclick href with variable


I have a Spring Boot with Thymeleaf project and I'm having trouble writing a button's onclick. I want the resulting url to be e.g. /owners/1/edit. I've tried a number of concatenations the latest being:

th:onclick="'window.location.href=\'/owners/{id}/edit(id=${owner.id})\''"

...but it doesn't evaluate properly, displaying only /owners/{id}/edit(id=${owner.id})

Any ideas?


Solution

  • Use <a> tag instead of onClick.
    Your code will look something like this -

        <a th:href="@{/owners/{id}/edit(id=${owner.id})}"> 
             <button type="button"> some button text </button> 
        </a>