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

103
Views
mostrar múltiples valores de la matriz de datos en múltiples elementos de tabla HTML con JavaScript

Quiero mostrar varios valores de la matriz de datos en varios elementos de la tabla HTML a la vez. Y el valor de los datos va a cambiar y debe sobrescribirse en HTML. Intenté algo como a continuación usando forEach anidado pero no funciona. ¿Qué tengo que hacer?

appendchild pero crea nuevas celdas a medida que se renueva el valor de los datos y no lo quiero.

HTML

 <table> <thead>something</thead> <tbody> <tr id="changeThis"> <th>row head</th> <td>0</td><!-- 1 --> <td>0</td><!-- 2 --> <td>0</td><!-- 3 --> <td>0</td><!-- 4 --> </tr> </tbody> </table>

js

 const data = [1, 2, 3, 4] const changeThis = document.querySelector("#changeThis"); changeThis.forEach(cellItem => { data.forEach(dataItem => { cellItem.innerHTML = dataItem }) })

¡Gracias por tu ayuda!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede usar querySelectorAll para seleccionar todos los tds y agregar su contenido

 const data = [1, 2, 3, 4] const changeThis = document.querySelectorAll("#changeThis td"); changeThis.forEach((cellItem,idx) => { cellItem.innerHTML = data[idx]; })
 <table> <thead>something</thead> <tbody> <tr id="changeThis"> <th>row head</th> <td>0</td><!-- 1 --> <td>0</td><!-- 2 --> <td>0</td><!-- 3 --> <td>0</td><!-- 4 --> </tr> </tbody> </table>

about 4 years ago · Juan Pablo Isaza Report

0

tambien puedes hacer eso...

 const data = [1, 2, 3, 4] , changeThis = document.querySelector('#changeThis') ; let index = 0; for (let cell of [...changeThis.cells]) { if ( cell.matches('td')) cell.textContent = data[index++] }
 <table> <thead>something</thead> <tbody> <tr id="changeThis"> <th>row head</th> <td>0</td><!-- 1 --> <td>0</td><!-- 2 --> <td>0</td><!-- 3 --> <td>0</td><!-- 4 --> </tr> </tbody> </table>

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!