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');
Who is Participating?
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 for table handling using javascript
Michel PlungjanIT ExpertCommented:
or just
var table = document.getElementById("myTable");  
var rows = table.rows
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.