tags:

views:

55

answers:

3

Hi,

Could someone show me how to loop through the anchor tags within a div and find the next sibling?

I have this so far.

var menu = document.getElementById('menu');
var links = menu.getElementsByTagName('a');

How do i loop through the links? Can i use .nextSibling to find if the next sibling is a div?

A: 
for(var i=0; i<links.length; i++) {    
if (links[i].nextChild.nodeName=="DIV")
    alert("This is a DIV")
}
infinity
nextChild isn't a property of a Node: https://developer.mozilla.org/en/DOM/Node
subhaze
A: 

Continuing your code:

for (var i in links) {
  alert(links[i]);
}

If I understood correctly, the mission was to loop through the anchors in the div.

bazmegakapa
+2  A: 

The nextSibling property of DOM nodes works perfectly in all browsers and does exactly what you'd expect. If there is no next sibling, it returns null.

Iterating over a NodeList (which is what getElementsByTagName returns) is identical to iterating over an array. The following will iterate over the links and alert each time it finds one whose next sibling is a <div>:

var menu = document.getElementById('menu');
var links = menu.getElementsByTagName('a');

// Iterate over the links
for (var i = 0, len = links.length, link; i < len; ++i) {
    link = links[i];
    if (link.nextSibling && link.nextSibling.nodeName == "DIV") {
        alert("Next sibling is DIV! " + link.innerHTML);
    }
}

Note that in non-IE browsers, whitespace between elements in HTML is considered a text node. You may want to ignore these whitespace nodes when considering what the next sibling of each link is. The following will do that:

function isWhitespace(node) {
    return node.nodeType == 3 && /^\s*$/.test(node.data);
}

// Iterate over the links
for (var i = 0, len = links.length, link, next; i < len; ++i) {
    link = links[i];
    next = link.nextSibling;
    if (next && isWhitespace(next)) {
        next = next.nextSibling;
    }
    if (next && next.nodeName == "DIV") {
        alert("Next sibling is DIV! " + link.innerHTML);
    }
}
Tim Down
This solution is almost correct. However, it doesn't take the A tags Text node into consideration. Here is a JSFiddle demonstrating: http://www.jsfiddle.net/subhaze/ytCxS/
subhaze
@subhaze: Well, I suppose you could say it was incorrect for that reason, but a text node is still a node as much as any other node, even if it does only contain whitespace. The real issue is that IE does not count whitespace text nodes as actual DOM nodes whereas all other browsers do. It's definitely worth pointing out, so I'll amend my answer.
Tim Down
@subhaze: Amended my answer. I think you'd only want to ignore whitespace text nodes, not any text node (as in your jsfiddle example).
Tim Down
@Tim Down: I believe what you wrote is correct, which is why I up voted it. But assumed, though I probably shouldn't have, was that they didn't want to take the Text node into consideration. My response was poorly written, I should have just stated "Do this if you wish to bypass the Text nodes" instead of insinuating that your answer was not correct. Sorry about that.
subhaze
@subhaze: You made a good point. No need to apologize for anything.
Tim Down
@Tim Down: Agreed, and nice work. Definitely tops mine.
subhaze