4
Contributors
9
Replies
28
Views
3 Years
Discussion Span
Last Post by Siberian
1

Try replacing your js with this...

var one = document.getElementsByTagName("a");
if (one.length) 
{
    alert("correct, four anchors"); 
} 
else 
{
    alert("wrong");
}

http://jsfiddle.net/8GY37/4/

Edited by JorgeM

1

What do you mean by "not working"? When you ask or report a bug or not working program, you should explain 3 things -- 1)Input data, 2)Expected output, and 3)What error you are getting, and 4)Explain what is not working (i.e. no alert display, the browser hung up, etc).

At the glance, the script should work BUT it could cause an unexpected behavior. If the page does not contain an anchor tag, you will get "undefined" which can't be used with length() function. You need to think about what you are testing...

Votes + Comments
nods
0

It works ! What if I removed an anchor tag can I use an array to check how many anchors there are ?

0

one.length is being used. When I remove an anchor the condition tells me there is four anchors when there are only three.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.