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

175
Visualizações
Code for highlighting table cell code isn't working

I have a code that's supposed to show the table cell value when clicked on and also highlight the table cell that has been clicked on (Using CSS class). However, the table cell isn't highlighting for some reason. It works when I remove the table cell value code. I'm not sure what's wrong.

const myWords = ["LOL", "HEY", "TOYS", "YES", "SIR", "JOY"];

//Highlighting Table Cell code
//Goes through every 'td' on page
for (let node of document.querySelectorAll("td")) {
  node.onclick = function() {
    if (node.className == "") {
      node.className = "selected"
    } else {
      node.className = ""
    }
  }
}

//Get Table Value code
var selectedWord = "";
var clicking = document.getElementsByTagName('td');

//Everytime the user click on a cell...
for (var i = 0; i < clicking.length; i++) {
  var click = clicking[i];
  click.onclick = function() {
    selectedWord += this.innerHTML;
    alert(selectedWord);
    //Calls a function for each word in myWords 
    myWords.forEach(myFunction);

    function myFunction(item, index) {
      //If a word matches the selected word...
      if (item == selectedWord) {
        alert(item);
      }
    }
  }
}
about 4 years ago · Juan Pablo Isaza
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