Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
33
rated 0 times [  40] [ 7]  / answers: 1 / hits: 16218  / 12 Years ago, tue, january 29, 2013, 12:00:00

i'm trying to loop through all the cells in a table and do a comparison on the value.



            var table = document.getElementById(assignedvlans);
alert(table);
alert($('#assignedvlans tbody tr').length);
for (var i = 0, cell; cell = table.cells[i]; i++) {
//iterate through all cells in table.
alert('in the loop');
alert(cell.val());
if (cell.val == IdforVlanToAdd)
{
alert('This vlan is already associated with the port.');
$bexit = true;
break;
}
}


When i test this code, the alert(table) code is working - it returns object HTMLTableElement and the alert for the table lengths returns 4, which is also correct.
But the alert statements inside the loop never happen.
Can you tell me where i'm going wrong with the loop control?
Thanks.


More From » jquery

 Answers
-23

table contains rows[], which themselves contain cells[]. You can't get the cells[] directly from the table.



You could use table.getElementsByTagName('td') as a shortcut, provided there are no nested tables.



Otherwise, you should loop through each of the rows[] and in that loop you can loop through the cells[].



var table = document.getElementById('assignedvlans'),
rows = table.rows, rowcount = rows.length, r,
cells, cellcount, c, cell;
for( r=0; r<rowcount; r++) {
cells = rows[r].cells;
cellcount = cells.length;
for( c=0; c<cellcount; c++) {
cell = cells[c];
// now do something.
}
}

[#80543] Monday, January 28, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leog

Total Points: 225
Total Questions: 113
Total Answers: 118

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
;