Add children in jQuery without leaving the chain

Is it possible to add children in a jQuery set to a set without leaving the chain?

Ie, leaving him, would look like this:

var set = $('.some-elements');
set.add('*', set);
// or immediate ones only
set.add(set.children());

I am looking for a way to do any of these without creating an intermediate variable set:

$('.some-elements').jqMagic("*");

(Of course, you can write the “jqMagic” plugin for this. I just want to make sure that the jQuery core does not have this feature.)

+3
source share
1 answer

I believe AndSelf will allow you to do what you ask.

update example:

Given the following html:

<div id="1" class="testdiv">
   <a href="#">test a</a>
   <a href="#">test b</a>
</div>


<div id="2" class="testdiv">
    <a href="#">test c</a>
    <a href="#">test d</a>
</div>

This code gives only children (anchor tags in this case) a red frame:

<script language="javascript" type="text/javascript">

    $('.testdiv').children().css('border','solid 1px red');

</script>

( andSelf()) div divs, divs - , :

<script language="javascript" type="text/javascript">

    $('.testdiv').children().andSelf().css('border','solid 1px red');

</script>
+6

All Articles