Jump to content

dealing with getElementsByTagName


ohdang888

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.