Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3411
  • Last Modified:

trying to traverse a HTML table using DOM

var deviceCbTable = document.getElementById(rows[i].id+'_CheckBoxList1');

var deviceCBTableRows = deviceCbTable.getElementsByTagName('tr');

//var deviceCBTableRowsTD = deviceCBTableRows.getElementsByTagName('td');
var deviceCBTableRowsTD = deviceCBTableRows.children;

I get the table using its ID. Then i get the TR in the table by getElementsByTagName
BUT i cant get the same to work for the table cells.
You will notice that i have tried 2 method of getting to the TD cells - one getElementByTagName and one
by using the children property. Also tried childNodes.

THis is in IE 6 ..                        
                                                //if(deviceCBTableRowsTD)alert('ye');else alert('n');
2 Solutions
Here is an example. Hope this is what you are looking for:

                  <title>Script Demo Gops</title>
                  <script language="javascript">
                        function readTable(){
                              var deviceCbTable = document.getElementById('tbl_CheckBoxList1');
                              for(var i=0;i<deviceCbTable.rows.length;i++){
                                    for(var j=0;j<deviceCbTable.rows[i].cells.length;j++){
                                          alert("Contents of Row "+(i+1)+"\n "+deviceCbTable.rows[i].cells[j].innerHTML)
<table id="tbl_CheckBoxList1" border="1" width="40%">
            <td>R1 C1</td>
            <td>R1 C2</td>
            <td>R1 C3</td>
            <td>R2 C1</td>
            <td>R2 C2</td>
            <td>R2 C3</td>
            <td>R3 C1</td>
            <td>R3 C2</td>
            <td>R3 C3</td>

<input type="button" value="Read Table" onclick="readTable();">
you can traverse the table/rows as
var table = document.getElementById("myTable");  
var rows = table.getElementsByTagName("tr");  
totalrows = rows.length;

for traversing rows/cells we have do as
for(var i=0;i<totalrows.length;i++){
  var x=totalrows[i].cells;

for detailed info/tutorial/how to see this
http://www.w3schools.com/htmldom/dom_obj_table.asp for table handling using javascript
Michel PlungjanIT ExpertCommented:
or just
var table = document.getElementById("myTable");  
var rows = table.rows

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now