Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend jquery $(this)

How to just extend selector from $(this) to, lets say, it's children? Or to anything else. I mean, how to properly contcatenate them with $(this)?

Like:

$("li.nav").each(function() {
    $(this AND children of this, or this AND it's siblings).something();
});

Sorry if this was already answered, couldn't find it.

like image 586
cincplug Avatar asked Mar 20 '12 12:03

cincplug


1 Answers

You can use andSelf to accomplish this:

$(this).children().andSelf().something();

You can also use .end to pop the last filtering operation off of the current chain. So if you wanted children and siblings, you could accomplish that too:

$(this)
    .children()    // children of "this"
    .end()         // now we're back to "this"
    .siblings()    // siblings of "this"
    .andSelf()     // and "this" itself
    .something();
like image 193
Andrew Whitaker Avatar answered Oct 10 '22 15:10

Andrew Whitaker