简体   繁体   中英

how to select a range of html table cell value in javascript?

Please help. I have a html table and for example I want to select the first 4 cell value of the table but I don't know how: I tried. var table = document.getElementById("td") selectedCell = table:cells[0:4]

javascript does not understand this way.

You can access a cell by its row index and cell index like this:

 var table = document.getElementById('table1') for(let rowIndex = 0; rowIndex < 2; rowIndex++){ for(let cellIndex = 0; cellIndex < 2; cellIndex++){ console.log(table.rows[rowIndex].cells[cellIndex]); } }
 <table id="table1" style="width:100%"> <caption>Monthly savings</caption> <tr> <th>Month</th> <th>Savings</th> </tr> <tr> <td>January</td> <td>$100</td> </tr> <tr> <td>February</td> <td>$50</td> </tr> <tr> <td>March</td> <td>$70</td> </tr> <tr> <td>April</td> <td>$160</td> </tr> </table>

if you give some sample data and an expected output I can use it to help you to make adjustment to this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM