0

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.

2
  • That example link doesn't have any text in, and it's invalid (/>...) Commented Jul 15, 2010 at 6:47
  • And "innerHtml" should be spelt "innerHTML" Commented Jul 15, 2010 at 6:49

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

@MMRUser: well, you have to make sure that the links have that class, to be able to access them by it. also, as others have pointed out, it should be innerHTML, not innerHtml, and your example link does not have an inner html.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.