Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

350
Visualizações
How to call JavaScript function from Thymeleaf-style-background?

I have a table with cells and want to move background settings for each cell to JavaScript. How to do it? For now I can't reach function color1

<head>
    <script language="JavaScript">
        function color1(row, col){
          if(isOdd(row) && isOdd(col)) return "#f4cd8d";
          if(isEven(row) && isEven(col)) return "#745853";
        }    
        
        function isEven(n) {
           return n % 2 == 0;
        }
        
        function isOdd(n) {
           return Math.abs(n % 2) == 1;
        }  
    </script>       

</head>
<body>  

<table id="chessboard" border="1" cellspacing="0" cellpadding="0">
    <tr height="30"
        th:each="row, rStat : ${#numbers.sequence(1, 8)}">
        <td width="30"  th:each="col, cStat : ${#numbers.sequence(1, 8)}"            
            th:data-row=${rStat.count} 
            th:data-col=${cStat.count}             
            th:style="'background: onload="javascript:color1(${rStat.count},${cStat.count})";'"> 
        </td>
    </tr>
</table>
</body>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

try calling JavaScript like this:

<table id="chessboard" border="1" cellspacing="0" cellpadding="0">
<tr height="30"
    th:each="row, rStat : ${#numbers.sequence(1, 8)}">
    <td th:id="${id}" width="30"  th:each="col, cStat : ${#numbers.sequence(1, 8)}"            
        th:data-row=${rStat.count} 
        th:data-col=${cStat.count}             
        th:attr="onload='color1(${rStat.count},${cStat.count})'"> 
    </td>
</tr>
</table>

and in javascript you can set the style of the field

function color1(row, col){
    var cell = document.querySelector('#id');
    var color;
    if(isOdd(row) && isOdd(col)) color = "#f4cd8d";
    if(isEven(row) && isEven(col)) color = "#745853";
    cell.style.background = color;
} 
about 4 years ago · Juan Pablo Isaza Relatório

0

Since you're already using JQuery, why not use the document.ready() to assign colors like this? No need for any JavaScript on the table itself.

<script>
$(document).ready(function () {
  $('.chessboard-square').each(function () {
    let row = parseInt($(this).data('row'));
    let col = parseInt($(this).data('col'));
    $(this).css('background-color', color1(row, col));
  })
});
</script>

<!-- Table -->
<table id="chessboard" border="1" cellspacing="0" cellpadding="0">
  <tr height="30" th:each="row, rStat : ${#numbers.sequence(1, 8)}">
    <td class="chessboard-square" width="30" th:each="col, cStat : ${#numbers.sequence(1, 8)}"
        th:data-row="${rStat.count}"
        th:data-col="${cStat.count}">
    </td>
  </tr>
</table>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda