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! Quote Link to comment 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]); } Quote Link to comment 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. Quote Link to comment 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]. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.