Switch
Switch

Reputation: 15453

HTML Javascript | how to get a value of a links array

I have a small html code base that produces a list of unique links, I want to get each an every link's text value using java script (not the link only the text).

my sample code (doesn't work)

for (var x = 0; x < document.getElementById("folderLink").length; x++) {
       alert(document.getElementById("folderLink")[x].innerHtml);
} 

HTML

<a id="folderLink" href="somelink">Dynamic Text Text Text</a>

assume that this is a one link of the list.

Thanks.

Upvotes: 0

Views: 598

Answers (1)

David Hedlund
David Hedlund

Reputation: 129792

getElementById will only ever return one element, regardless of how many there may be with that ID. Because there should only ever be one element of any ID.

So you can't access its length because it's not a collection it's a DOM node.

You could use getElementsByClassName

Upvotes: 3

Related Questions