How to select children and self with jQuery?
<p>I'm wondering what the best way is to find children of an element in jQuery, but also include the parent in the 'find'.</p>

<p>Here's my simplified basic HTML setup:</p>

<pre><code>&lt;div id="container"&gt;
    &lt;form id="form1"&gt; &lt;!-- form 1 content --&gt; &lt;/form&gt;
    &lt;form id="form2"&gt; &lt;!-- form 2 content --&gt; &lt;/form&gt;
&lt;/div&gt;
</code></pre>

<p>And I want a function like this...</p>

<pre><code>function getForms ($container) 
{
    // Option 1
    var $allForms = $container.find('form').andSelf().filter('form');

    // Option 2
    var $allForms = $container.find('form');
    if ($container.is('form')) $allForms.add($container);

    // Should return all the forms in the container if passed $('#container')
    // Or return just one form if passed $('#form1')
    return $allForms;
}
</code></pre>

<p>I'm fairly certain that both option 1 or 2 will work. Does anyone know which option above is more efficient? Are there other options which are more elegant or more efficient?</p>

<p><strong>EDIT:</strong> 
I wasn't happy with the <em>.is()</em> in option 2 because it didn't work when the container had multiple jQuery objects in it. So I came up with something like this:</p>

<pre><code>// Option 3
function getContainerElements ($container, selector) {
    return $container.find(selector).add($container.filter(selector));
}
</code></pre>

<p>I haven't tested it too much, but I think it'll work for all general cases.</p>
