Dancing Around The DOM

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

Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg
  • Reddit
  • Furl
  • NewsVine
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • De.lirio.us
  • Fark
  • feedmelinks
  • LinkaGoGo
  • Ma.gnolia
  • Netvouz
  • RawSugar
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

23 Comments to “Dancing Around The DOM”  

  1. 1 corra24

    Interesting post.

    Thanks.

  2. 2 rocks123

    good post want to try it ,do we have a demo for this?

  3. 3 rocks123

    what are the other browsers protoype supports other than IE ?

  4. 4 Aron

  5. 5 fdgdfgfdg

    iopuhjfhgjfghjdfgdfertytyytuuyiioiupwqeretreryttyutuiyuoiuiuioljklhghfhghdsfgadadffdsgvbcxvnbvmghghghhghfdfdfgfdfgfdgfdgfgdfg
    fdgdfg

  6. 6 fdgdfgfdg

    ghjgfjhfgjhfgjhghjfgjhhj

  7. 7 Leroy Gill

    0d8isbxun8h44k7x

  1. 1 linzer tarts
  2. 2 Tramadol.
  3. 3 Order phentermine uk.
  4. 4 Wo kann ich filme downloaden?
  5. 5 Adderall xr.
  6. 6 Cheap vicodin cod.
  7. 7 iContent Robot
  8. 8 Recently leaked footage of the new Paris Hilton sex tape
  9. 9 Buy rohypnol.
  10. 10 Bqeqkfndvqn
  11. 11 Propecia.
  12. 12 Generic for diflucan.
  13. 13 Rohan
  14. 14 Taylor
  15. 15 Alondra
  16. 16 Jayla


Leave a Reply