简体   繁体   中英

Read table cell values(<td>) using table row(<tr>) id with Javascript only

I have a table as follows:

<table id="mytable">
  <tr id='id1'>
    <td>Cell A</td>
    <td >Cell B</td>
    <td >Cell C</td>
  </tr>
  <tr id='id2'>
    <td >Cell D</td>
    <td>Cell E</td>
    <td>Cell F</td>
  </tr>
</table>

I know the row id id1 I want read all the cells I mean <td> of that particular using table id and row id. That means I need to read values Cell A, Cell B, Cell C

Note: use Javascript only

Here is the demo fiddle .

HTML

<table id="mytable">
  <tr id='id1'>
    <td>Cell A</td>
    <td >Cell B</td>
    <td >Cell C</td>
  </tr>
  <tr id='id2'>
    <td >Cell D</td>
    <td>Cell E</td>
    <td>Cell F</td>
  </tr>
</table>

JS

var tr = document.getElementById('id1');
var td = tr.getElementsByTagName("td");
for(var i=0;i<td.length;i++) {
    console.log(td[i].innerHTML);
}

Hope this is what you need!

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