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

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

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 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