Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

438
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda