Learn how to a build a cloud-first strategyRegister Now

x
  • 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');
0
deersuper
Asked:
deersuper
2 Solutions
 
gops1Commented:
Here is an example. Hope this is what you are looking for:

<html>
      <head>
            <head>
                  <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)
                                    }
                              }
                        }
                  </script>
            </head>
<body>
<table id="tbl_CheckBoxList1" border="1" width="40%">
      <tr>
            <td>R1 C1</td>
            <td>R1 C2</td>
            <td>R1 C3</td>
      </tr>
      <tr>
            <td>R2 C1</td>
            <td>R2 C2</td>
            <td>R2 C3</td>
      </tr>
      <tr>
            <td>R3 C1</td>
            <td>R3 C2</td>
            <td>R3 C3</td>
      </tr>
</table>
<br>

<input type="button" value="Read Table" onclick="readTable();">
</body>
</html>
0
 
sundaramkumarCommented:
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
0
 
Michel PlungjanIT ExpertCommented:
or just
var table = document.getElementById("myTable");  
var rows = table.rows
0

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