Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

435
Views
How can I call javascript function in thymeleaf th:if?

I want to call javascript function in 'th:if'. This is my code.

<li th:each="dto, stat : ${list}" th:if="${stat.count>10}">
   <span th:if="'javascript:isNew('+${dto.regDts}+');'">blah blah</span>
</li>

...

<script th:inline="javascript">
 function isNew(date) {
     if (...) {
        return true;
     }
     return false;
 }
</script>

but it doesn't call isNew().

so when I wrote like this...

 <span th:if="isNew(${dto.regDts});">blah blah</span>

i got an error.

how can I call javascript function in "th:if"?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Thymeleaf generates HTML in the server and passes that to the browser. At that point, Thymeleaf is no longer "active". JavaScript runs in the browser using the HTML that Thymeleaf has generated. So it is impossible to do what you want like this.

The easiest solution is to add an extra method or property on the Java object that is used. Suppose you add an isNew() method on your dto, then you can do:

<li th:each="dto, stat : ${list}" th:if="${stat.count>10}">
   <span th:if="${dto.new}">blah blah</span>
</li>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!