ohdang888 Posted May 21, 2009 Share Posted May 21, 2009 i'm using getElementsByTagName to grab all links and go through them and alert their ids Its working fine, except for the fact that it goes through the first link on the page 4 times... in other words, instead of alerting like this: 1 2 3 it alerts like this: 1 2 3 1 1 var links = document.getElementsByTagName("a"); for (var x in links) { var link = links.item(x); alert(link.id); } and this <a href="index1.php" id="1">1</a> <a href="index2.php" id="2">2</a> <a href="index3.php" id="3">3</a> Any ideas? Thanks! Link to comment https://forums.phpfreaks.com/topic/159094-dealing-with-getelementsbytagname/ Share on other sites More sharing options...
Ken2k7 Posted May 21, 2009 Share Posted May 21, 2009 What's item? I never seen that before. for ... in loops should not be used on an indexed array. Use a standard for loop or a while loop. for (var e = links.length; --e > -1; ) { alert(links[e]); } Link to comment https://forums.phpfreaks.com/topic/159094-dealing-with-getelementsbytagname/#findComment-838989 Share on other sites More sharing options...
ohdang888 Posted May 21, 2009 Author Share Posted May 21, 2009 item() has to be used to draw a value from an object. getElementsByTagName creates an object, not an array. thanks for the suggestion, modified it to this: for (var e = links.length; --e > -1; ) { alert(links.item(e).id); } and it worked. Link to comment https://forums.phpfreaks.com/topic/159094-dealing-with-getelementsbytagname/#findComment-838996 Share on other sites More sharing options...
Ken2k7 Posted May 21, 2009 Share Posted May 21, 2009 I still don't know item. Looks like a longer way of writing array[index]. Link to comment https://forums.phpfreaks.com/topic/159094-dealing-with-getelementsbytagname/#findComment-839007 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.