Dancing Around The DOM
Published December 3rd, 2006 in GeneralWhenever I’m coding a site, I spend a lot of time in the DOM. For some odd reason, I tend look up the built-in JavaScript methods the most. For instance, I can never remember the parentNode method name. Lucky for me, I don’t have to anymore thanks to to Prototype. As of v1.5.0_rc1, Prototype has added some connivance methods for navigating the DOM.
These methods include things such as: ancestors, descendants, up, down, next, previous, previousSiblings, nextSiblings, and siblings.
Here is example markup we will be working with:
<div id="app">
<ul id="nav">
<li id="main"><a href="/main.html">main</a></li>
<li id="articles"><a href="/articles.html">articles</a></li>
<li id="examples"><a href="/examples.html">examples</a></li>
</ul>
<div id="content">
. . . content . . .
</div>
</div>
In the interest of time and text, I’m not going to cover how to update the content div element using AJAX. But, in order to show a trivial use of the DOM methods in Prototype, I will demonstrate how the to add classes to the markup when the user clicks on different links.
We are going to do this all unobtrusively. We’re going to start by attaching ‘onclick’ events to the anchor tags.
function attachOnclicks(event) {
$('nav').descendants().each(function(element) {
if(element.tagName == "A") {
Event.observe(element, "click", navOnclick, false);
}
});
}
Event.observe(window, "load", attachOnclicks, false);
The Event.observe call, works like this: observe the window object for load Events and when a load event occurs call attachOnclicks. You can pretty much ignore the ‘false’ at the end of method call, it has to do with event bubbling, and (as of yet) it isn’t supported that well across browsers.
Once the page has loaded, the ‘attachOnclicks’ method gets called. ‘attachOnclicks’ gets the element with an id of ‘nav’, which is the ul tag. It then calls one of those special DOM methods, descendants. The descendants call returns all children, recursively, of the ul element. It then enumerates over each descendant to find the anchor tags. There is then another Event.observe call, that gets triggered 3 times (once for each anchor). This catches all click events on the anchor tag and calls the ‘navOnclick’ function when it receives them.
function navOnclick(event) {
Event.stop(event);
var clickedLink = Event.element(event);
var clickedLi = clickedLink.up();
clickedLi.siblings().each(function(sibling) {
sibling.removeClassName("active");
});
clickedLi.addClassName("active");
}
And finally we have the ‘navOnclick’ function. navOnclick takes an argument of event that is passed in by the browser when it gets a click event (you don’t need to do anything, this happens automatically). The first line stops the browser from navigating to the anchor the user clicked. The second line figures out which link the user clicked. It does this by calling the prototype function Event.element, it takes an event as a parameter that you provide. Luckily, you can just pass in the event that you received from the browser.
Then we get to use another DOM method, up. The call to up returns the li tag that surrounds the clicked link. Once we have the li tag a quick enumeration over the siblings removes the “active” css class from all of the other li tags.
Finally, we add the active css class to the clicked li tag. This gives you the ability to style #ul li.active to look different from the other li tags, showing the user which page they are viewing. This post ended up being a how-to of events just as much as DOM walking, but that’s okay, since it does show some of the power of these newer DOM methods.
23 Comments to “Dancing Around The DOM”
- 1 Trackback on Apr 26th, 2007 at 7:19 pm
- 2 Trackback on Feb 7th, 2008 at 5:01 am
- 3 Trackback on Mar 6th, 2008 at 2:33 am
- 4 Trackback on Mar 22nd, 2008 at 2:01 pm
- 5 Trackback on Mar 22nd, 2008 at 2:41 pm
- 6 Trackback on Mar 22nd, 2008 at 4:26 pm
- 7 Trackback on Apr 22nd, 2008 at 9:23 pm
- 8 Trackback on May 19th, 2008 at 6:15 pm
- 9 Trackback on Jul 16th, 2008 at 1:16 am
- 10 Trackback on Aug 17th, 2008 at 4:28 pm
- 11 Trackback on Aug 20th, 2008 at 11:35 pm
- 12 Trackback on Aug 29th, 2008 at 5:10 am
- 13 Trackback on Aug 10th, 2009 at 8:28 am
- 14 Trackback on Aug 10th, 2009 at 8:29 am
- 15 Trackback on Aug 10th, 2009 at 8:56 am
- 16 Trackback on Aug 10th, 2009 at 9:19 am

























Interesting post.
Thanks.
good post want to try it ,do we have a demo for this?
what are the other browsers protoype supports other than IE ?
iopuhjfhgjfghjdfgdfertytyytuuyiioiupwqeretreryttyutuiyuoiuiuioljklhghfhghdsfgadadffdsgvbcxvnbvmghghghhghfdfdfgfdfgfdgfdgfgdfg
fdgdfg
ghjgfjhfgjhfgjhghjfgjhhj
0d8isbxun8h44k7x