JQuery: working with the wrapped set – part III

Published Fri, Jul 17 2009 13:40

Today we’re going to keep looking at JQuery methods that interact with the wrapped set and we’re going to take a closer look at what I like to call relationship methods. I call them that because these methods allow you to take the current hierarchy into account.

For instance, if you need to get the unique children of the wrapped nodes of an existing JQuery object, you can simply use the children method. Notice that you’ll get a unique collection that wraps all the children of the initial JQuery object’s wrapped node. Take a look at the following code:

var a = $("select");
var b = a.children();

The b JQuery object will end up wrapping all the children of a’s wrapped set. If you want, you can also pass a selector into the children method. This is nice when you only want to restrict the new object to a specific type of element.

You can also go in the other direction, ie, you can get the parent of all elements of a wrapped set by using the parent method. There’s also a parents method which gets the ancestors of all wrapped elements. Notice that you’ll always end up with a wrapped set that contains unique objects. Its usage is really similar to the previous example, as you can see from the following snippet:

var a = $("select");
var b = a.parent();

You can also go “sideways”. next and prev returns the next and previous siblings ensuring uniqueness. nextAll and prevAll do the same but they don’t filter duplicate items. Finally, there’s the siblings method, which simply returns all the existing siblings. The usage of these methods is  really similar to the previous ones, so I’ll leave the code for you.

And that’s all for this post. Keep tuned for more on JQuery.

Filed under:

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above:  

Search

This Blog

Tags

Community

Archives

Syndication

Email Notifications

News




  • View Luis Abreu's profile on LinkedIn


    Follow me at Twitter

    My books

    Silverlight 4.0: Curso Completo

    ASP.NET 4.0: Curso Completo

    Portuguese LINQ book cover

    Portuguese ASP.NET 3.5 book cover

    Portuguese ASP.NET AJAX book cover

    Portuguese ASP.NET AJAX book cover