Why isn't this loop working ?

Dani AI

Generated

Short version: the loop itself was fine — the confusion was how the result was checked and when the check ran. was right to suggest avoiding document.write for debugging, and pointed you toward checking the count. Two practical mistakes to watch for: using length() (with parentheses) instead of the length property, and using if (one.length) when you actually want to see the numeric count (that condition only tests for nonzero).

A few debugging and accuracy tips that weren't spelled out in the thread:

  • Check timing: if your script runs before the page is parsed you will not see the anchors you expect. Run the code after DOMContentLoaded or put the script at the end of the body.
  • Know the difference between live and static collections: getElementsByTagName returns a live HTMLCollection (it reflects DOM changes), while querySelectorAll returns a static NodeList snapshot. See the spec/notes on these behaviors at MDN: Document.getElementsByTagName and Document.querySelectorAll.
  • Use the console to verify what the DOM actually contains at the moment your code runs. Evaluate something like document.querySelectorAll('a').length to get an immediate count, inspect the returned list in DevTools, and confirm there are no hidden anchors in HEAD or injected by other scripts.

If you need a static array to iterate over (so later DOM removals don't change your loop), convert the collection to an array before looping. The above checks and the links explain the live/static behavior so you know which approach fits your use case. Thanks to for reminding about describing input/output and error symptoms — that makes debugging much faster.

Recommended Answers

All 9 Replies

Works for me, provided you change write to alert

It's not working on my end. The code won't work in JSBin either !

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/

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...

commented: nods +14

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

Use one.length

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

So use the one.length property within the alert method.

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

alert("there are " + one.length + " anchors");

Oh, thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.