Programming4us
         
 
 
Programming

jQuery 1.3 : Developing plugins - DOM traversal methods

11/19/2010 2:34:08 PM
In some cases, we may want a plugin method to change which DOM elements are referenced by the jQuery object. For example, suppose we wanted to add a DOM traversal method that found the grandparents of the matched elements:
jQuery.fn.grandparent = function() {
plug-ins, developingDOM traversal method, addingvar grandparents = [];
this.each(function() {
grandparents.push(this.parentNode.parentNode);
});
grandparents = jQuery.unique(grandparents);
return this.setArray(grandparents);
};

This method creates a new grandparents array, populating it by iterating over all of the elements currently referenced by the jQuery object. The standard DOM .parentNode property is used to find the grandparent elements, which are pushed onto the grandparents array. This array is stripped of its duplicates with a call to $.unique(). Then the internal jQuery .setArray() method changes the set of matched elements to the new array. Now, we can find and operate on the grandparent of an element with a single method call.

To test our method, we'll set up a deeply-nested<div> structure:

<div>Deserunt mollit anim id est laborum</div>
<div>Ut enim ad minim veniam
<div>Quis nostrud exercitation
<div>Ullamco laboris nisi
<div>Ut aliquip ex ea</div>
<div class="target">Commodo consequat
<div>Lorem ipsum dolor sit amet</div>
</div>
</div>
</div>
plug-ins, developingDOM traversal method, adding<div>Duis aute irure dolor</div>
<div>In reprehenderit
<div>In voluptate</div>
<div>Velit esse
<div>Cillum dolore</div>
<div class="target">Fugiat nulla pariatur</div>
</div>
<div>Excepteur sint occaecat cupidatat</div>
</div>
</div>
<div>Non proident</div>
</div>
<div>Sunt in culpa qui officia</div>


We'll identify the target elements (<div class="target">) by styling their text bold:

Now we can locate the items' grandparent elements by using our new method:

$(document).ready(function() {
$('.target').grandparent().addClass('highlight');
});

The highlight class correctly italicizes both grandparent items on the page:

However, this method is destructive. The actual jQuery object is modified as a side effect&mdash;one that becomes evident if we store the jQuery object in a variable:

$(document).ready(function() {
DOM traversal method, addingside effectvar $target = $('.target');
$target.grandparent().addClass('highlight');
$target.hide();
});

This code should highlight the grandparent elements, and then hide the target elements. However, the actual effect is that the grandparents are hidden instead:

The jQuery object stored in $target has changed to refer to the grandparent. To avoid this, we need to make the method nondestructive. This is made possible by the internal stack jQuery keeps for each object.

jQuery.fn.grandparent = function() {
DOM traversal method, addinginternal stack jQuery usedvar grandparents = [];
this.each(function() {
grandparents.push(this.parentNode.parentNode);
});
grandparents = jQuery.unique(grandparents);
return this.pushStack(grandparents);
};


By calling .pushStack() instead of .setArray(), we create a new jQuery object, rather than modifying the old one. Now the $target object is not modified, and the original target objects are hidden by our code:

As a side benefit, .pushStack() also allows the .end() and .andSelf() methods to work with our plugin, so we can chain methods together properly:

$(document).ready(function() {
$('.target').grandparent().andSelf().addClass('highlight');
});

DOM traversal methods such as .children() were destructive operations in jQuery 1.0, but became non-destructive in 1.1.


Other -----------------
- Using Cloud Services : Exploring Online Planning and Task Management
- Using Cloud Services : Exploring Online Scheduling Applications
- Using Cloud Services : Exploring Online Calendar Applications
- SOA with .NET and Windows Azure : Service Contracts with WCF (part 3)
- SOA with .NET and Windows Azure : Service Contracts with WCF (part 2)
- SOA with .NET and Windows Azure : Service Contracts with WCF (part 1)
- Cloud Security and Privacy : Data Security and Storage
- iPad SDK : Working with Documents - Desktop Synchronization
- Required Project Images for iPad Apps
- iPhone SDK : GameKit Voice Chat
- iPhone SDK : Creating Basic GameKit Services (part 2) : Sending and Receiving Data
- iPhone SDK : Creating Basic GameKit Services (part 1)
- iPad : Navigating with Maps
- Adding iPad to the Mix
- A Brief History of Legacy .NET Distributed Technologies : .NET Remoting
- A Brief History of Legacy .NET Distributed Technologies : .NET Enterprise Services
- iPad SDK : Outputting to an External Screen
- iPad SDK : Displaying Multiple Videos
- Parallel Programming Drivers
- Parallel Programming with Microsoft .Net : Parallel Loops - An Example
 
 
Top 10
- First Stages of SEO : Benchmarking Current Indexing Status
- Programming with SQL Azure : Connecting to SQL Azure (part 2)
- Exchange Server 2010 : Office Communication Server 2007 R2 Integration (part 3) - Deploying Instant Messaging for OWA
- Active Directory Domain Services 2008: Create Shadow Groups
- Windows 7 : Checking Your Computer’s Security Settings (part 1)
- Securing Windows 7 : Thwarting Snoops and Crackers (part 2) - Locking Your Computer Manually, Automatically
- SharePoint 2010 : Get to a Site’s Permission Management Page
- Windows Azure Table Overview (part 2) - Azure Tables Versus Traditional Databases
- Windows 7 : Customizing the New Menu
- Context and Interception : .NET Component Services
Most view
- Windows Server 2008 : Manage Terminal Services
- SharePoint 2010 : Enforce Custom Validation on a Column
- Programming WCF Services : Data Contracts - Collections (part 1) - Concrete Collections & Custom Collections
- Sharepoint 2010 : Add a Content Type to a List or Document Library
- Windows Firewall with Advanced Security in Windows Server 2008 (part 2)
- Windows Server 2008 : Configuring SMTP (part 6) - Using an SMTP Virtual Server
- Windows 7 : Removing Stored Remote Desktop Credentials
- Windows 7 : Thwarting Spam with Windows Live Mail’s Junk Filter (part 2) - Blocking Countries and Languages
- SharePoint 2010 : Using Data Connection Libraries (part 1) - Connecting to Data Using Alternative Credentials & Configuring the Secure Store Service
- Exchange Server 2007 : Work with Offline Address Books