javascript - How to add record to rth column of nth row table jquery? -
i have table having 6 columns of 5 poplated , last column rows empty.
now need add data in last column of each row , how loop on table using loop , same?
you can use rows
collection of table for of
statement, use .cells
collection on each row last one.
for (const row of document.queryselector("#mytable").rows) { row.cells[row.cells.length-1].textcontent = "some dynamic data"; }
table td:last-child { background: #ddd; }
<table id=mytable> <tbody> <tr><td>first</td><td>second</td><td>third</td><td>fourth</td><td></td> <tr><td>first</td><td>second</td><td>third</td><td>fourth</td><td></td> <tr><td>first</td><td>second</td><td>third</td><td>fourth</td><td></td> <tr><td>first</td><td>second</td><td>third</td><td>fourth</td><td></td> <tr><td>first</td><td>second</td><td>third</td><td>fourth</td><td></td> </tbody> </table>
or use queryselectorall
select last cell of each row directly.
for (const cell of document.queryselectorall("#mytable td:last-child")) { cell.textcontent = "some dynamic data"; }
table td:last-child { background: #ddd; }
<table id=mytable> <tbody> <tr><td>first</td><td>second</td><td>third</td><td>fourth</td><td></td> <tr><td>first</td><td>second</td><td>third</td><td>fourth</td><td></td> <tr><td>first</td><td>second</td><td>third</td><td>fourth</td><td></td> <tr><td>first</td><td>second</td><td>third</td><td>fourth</td><td></td> <tr><td>first</td><td>second</td><td>third</td><td>fourth</td><td></td> </tbody> </table>
you add :empty
selection select empty cells. document.queryselectorall("#mytable td:last-child:empty")
Comments
Post a Comment