PDA

View Full Version : checking id of divs in a loop for NS 6


martin_narg
07-20-2002, 10:16 AM
I'm trying to loop through all divs on a page and check their id for a phrase (ixx). I've used the code below which works fine for IE, but in Netscape 6 the return wasn't what I was expecting! I know for IE I loop through all the document, and in mozilla/netscape 6 I am just dumping the divs into an array to check in this example.

I'd greatly appreciated any help with this. I'm working out the W3C DOM stuff atm so apols if it's a bit of a noobie question.

Many thanks, script below:

<script>
var ie = (navigator.appName.indexOf('Microsoft') != -1) ? true : false;
var myArray = (ie) ? document.all : document.getElementsByTagName("div");

for( var i=0; i<myArray.length; i++ ) {
var str = new String(myArray[i].id);
if( str.indexOf('ixx') != -1 )
alert( str )
}
</script>

Martin

jkd
07-20-2002, 10:37 PM
var divs = document.getElementsByTagName('div');
for (var i = 0; i < divs.length; i++) {
if (divs.item(i).getAttribute('id').indexOf('someStr') > -1) {
// bla
}
}

Is how you would loop through all the div's looking for a certain substring of an id attribute in a DOM-compliant browser.

martin_narg
07-21-2002, 12:16 AM
thanks very much, exactly what i was lookin for

Martin