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

169
Visualizações
Change the background colour of a table cell if the content is not the same as another cell

I have an output.php. It creates an html table with 3 columns: Question number, Correct Answer, Student Answer. Works just as I want it to.

Now what I would like is to paint the cells with incorrect student answers red. I think Javascript is the best way to do this, rather than php.

Looking at other answers here, I was hoping this might do the job, but, alas ...

Can you please help me get this working?

<script type="text/javascript" >
function paint_cells() {
    var tds = document.querySelectorAll('tbody td'), i;
    for(i = 0; i < tds.length; i += 3) {
      if(tds[i+1].textContent != tds[i+2].textContent){
        tds[i+2].bgColor = "red";
      }
    }
</script>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I think you need to wait for page DOM loaded, try below. And it also depends on how and when the table in your page is generated, if it doesn't work, please provide more details.

<script type="text/javascript" >
    
    window.addEventListener('DOMContentLoaded', (event) => {
        var tds = document.querySelectorAll('tbody td'), i;
        for(i = 0; i < tds.length; i += 3) {
          if(tds[i+1].textContent != tds[i+2].textContent){
            tds[i+2].bgColor = "red";
          }
        }
    });
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

You code working good! I think your problem occurs that your js run before the dom already loaded. You have multiple opportunities to fix this. 1) you can add your script to the bottom inside the body tag. 2) work with onload event. https://developer.mozilla.org/de/docs/Web/API/GlobalEventHandlers/onload

Note Maybe you forgot to call the function? paint_cells()

function paint_cells() {
  var tds = document.querySelectorAll('tbody td'), i;
  for(i = 0; i < tds.length; i += 3) {
    if(tds[i+1].textContent != tds[i+2].textContent){
      tds[i+2].bgColor = "red";
    }
  }
}

paint_cells();
  
<table border="1">
  <thead>
    <tr>
      <th>Question</th>
      <th>Correct Answer</th>
      <th>Students Answer</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>abc</td>
      <td>right</td>
      <td>false</td>      
    </tr>
    <tr>
      <td>abc</td>
      <td>right</td>
      <td>right</td>                 
    </tr> 
    <tr>
      <td>abc</td>
      <td>right</td>
      <td>false</td>                 
    </tr>     
  </tbody>
</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