...

View Full Version : looping through dom elements



garry27
12-16-2006, 06:50 PM
hi,

i'm trying to loop through a table to find the next row with empty td elements. i've put alert events along the way to try and debug it (normally i'd have (txt=='') in the if control structure.

the first alert popup says [object text], the second says undefined. how do i change the code so nextEmptyRow reads the next empty row's index or id?



insRow:function(elm){
var tbl = elm.parentNode;
for (var i=0;i<tbl.rows.length;i++) {
var currTr = tbl.rows[i];
var td = currTr.firstChild;
var txt = td.firstChild;
alert(td);
if (txt == '3'){
var nextEmptyRow = '3';
break;
}
}
alert(nextEmptyRow);

whizard
12-16-2006, 07:30 PM
I'm not sure I can be helpful here, but does is this pages helpful to you at all?

http://dojotoolkit.org/pipermail/dojo-interest/2006-July/011832.html

Dan

Arbitrator
12-16-2006, 08:09 PM
Why donít you just put the alert inside the if statement? If you necessarily need to keep it outside of the if statement though, you can declare nextEmptyRow outside of the for statement instead of inside it so that the alert function can access it.

vegu
12-16-2006, 10:16 PM
Hrm, i dont think i understand what youre doing in your code, i mean i understand your problem but not the way youre going at it.

You want to find the first TD element inside a table that is empty? Or the first row that has an empty TD element? either ways this could probably help you:



insRow:function(elm){

var table = elm.parentNode
var i, td, currTr, nextEmptyRow;
for(i = 0; i < table.rows.length; i++) {
currTr = tbl.rows[i];
td = currTr.firstChild;
if (!td.childNodes.length){

/* no children found set nextEmptyRow to the id of the
* empty td
*/

nextEmptyRow = td.id;
break;
}
}
alert(nextEmptyRow);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum