Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
usando una variable en el selector de consulta n-th-child con javascript

Quiero revisar todas las filas de mi tabla y si la primera celda de una fila es igual a 1 o 15, quiero ocultar el valor en la sexta celda de esa fila. Tengo un problema con la variable i en thedocument.querySelector("tr:nth-child(i) td:nth-child(6)") porque parece que la variable no se reconoce. Sin embargo, cuando pongo un número real dentro del nth-child, funciona y oculta la fila correspondiente. Pero, obviamente, necesitaría usar la variable i para que la celda que se oculta esté en la fila cuya primera celda sea 1 o 15. Consulte el script a continuación:

 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 answers
Answer question

0

Como sugiere el usuario 1599011, está pasando el carácter i en su cadena de selector de consulta en lugar del valor de su variable i. Podrías usar una plantilla literal así:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!