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

166
Visualizações
using a variable in query selector nth-child with javascript

I want to go through all the rows in my table and if a row's first cell is equal to either 1 or 15, I want to hide the value in the sixth cell of that row. I'm having a problem with the i variable in thedocument.querySelector("tr:nth-child(i) td:nth-child(6)") because it seems like the variable is not being recognized. However, when I put an actual number inside the nth-child, it works, and it hides the corresponding row. But obviously I would need to use the i variable so that the cell that gets hidden is in the row whose first cell is either 1 or 15. Please see script below:

var ids = document.querySelectorAll("table tr td:nth-child(1)");
[].forEach.call(ids , function(cell, i) {

if(cell.innerHTML == 1 ){
document.querySelector("tr:nth-child(i) td:nth-child(6)").style.display = "none";
console.log("ito yung row in question: " + i);
}

if(cell.innerHTML == 15 ){
document.querySelector("tr:nth-child(i) td:nth-child(6)").style.display = "none";
console.log("ito yung row in question: " + i);
}

});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As user1599011 suggests, you are passing the i character in your query selector string rather than the value of your i variable. You could use a template literal like so:

var ids = document.querySelectorAll("table tr td:nth-child(1)");
[].forEach.call(ids , function(cell, i) {

if(cell.innerHTML == 1 ){
document.querySelector(`tr:nth-child(${i}) td:nth-child(6)`).style.display = "none";
console.log("ito yung row in question: " + i);
}

if(cell.innerHTML == 15 ){
document.querySelector(`tr:nth-child(${i}) td:nth-child(6)`).style.display = "none";
console.log("ito yung row in question: " + i);
}

});
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