Exforsys.com
 

Sponsored Links

 

jQuery Tutorials

 
Home Tutorials jQuery
 

jQuery Reference

 

DOM Traversal Methods

Page 3 of 4


DOM Traversal Methods

Sometimes we have a jQuery object that already references a set of DOM elements, but we need to perform an action on a different, related set of elements. In these cases, DOM traversal methods are useful. We can see this in part of our script:



Sample Code
  1. this.toggleClass('arrow-down')
  2.      .next()
  3.      .slideToggle('fast');
Copyright exforsys.com


Because of the context of this piece of code, the keyword this refers to a jQuery object (it often refers instead to a DOM element). In our case, this jQuery object is in turn pointing to the <h3> heading of the table of contents. The .toggleClass method call manipulates this heading element. The subsequent .next() operation changes the element we are working with, though, so that the following .slideToggle method call acts on the <div> containing the table of contents rather than its header. The methods that allow us to freely move about the DOM tree like this are listed in Chapter 3.


DOM Manipulation Methods

Finding elements is not enough; we want to be able to change them as well. Such changes can be as straightforward as changing a single attribute:


$chapterTitle.attr('id', chapterId);

Here we modify the ID of the matched element on the fly.  Sometimes the changes are further-reaching, on the other hand:


Sample Code
  1. $('<div id="page-contents"></div>')
  2.      .prepend('<h3>Page Contents</h3>')
  3.      .append('<div></div>')
  4.      .prependTo('body');
Copyright exforsys.com


This part of the script illustrates that the DOM manipulation methods can not only alter elements in place, but also remove, shuffle, and insert them. These lines add a new heading at the beginning of <div id="page-contents">, insert another <div> container at the end of it, and place the whole thing at the beginning of the document body. Chapter 4 will detail these and many more ways to modify the DOM tree.


Event Methods

Even when we can modify the page at will, our pages will sit in place, unresponsive. We need event methods to react to user input, making our changes at the appropriate time:


Sample Code
  1. $('#introduction > h2 a').click(function()
  2. {
  3.      $('#introduction').load(this.href);
  4.      return false;
  5. });
Copyright exforsys.com


In this snippet we register a handler that will execute each time the selected anchor tag is clicked. The click event is one of the most common ones observed, but there are many others; the jQuery methods that interact with them are discussed in Chapter 5.


Chapter 5 also discusses a very special event method, .ready:


Sample Code
  1. $(document).ready(function()
  2. {
  3.      // ...
  4. });
Copyright exforsys.com


This method allows us to register behavior that will occur immediately when the structure of the DOM is available to our code—even before the images have loaded.


Effect Methods

The event methods allow us to react to user input; the effect methods let us do this with style. Instead of immediately hiding and showing elements, we can do so with an animation:



Sample Code
  1. this.toggleClass('arrow-down')
  2.      .next()
  3.      .slideToggle('fast');
Copyright exforsys.com


This method performs a fast sliding transition on the element, alternately hiding and showing it with each invocation. The built-in effect methods are listed in Chapter 6, as is the way to create new ones.




Next Page: AJAX Methods


Read Next: jQuery Table Manipulation

 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape