I'm wondering what the best way is to find children of an element in jQuery, but also include the parent in the 'find'.
Here's my simplified basic HTML setup:
<div id="container">
    <form id="form1"> <!-- form 1 content --> </form>
    <form id="form2"> <!-- form 2 content --> </form>
</div>
And I want a function like this...
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;
}
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?
EDIT: I wasn't happy with the .is() 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:
// Option 3
function getContainerElements ($container, selector) {
    return $container.find(selector).add($container.filter(selector));
}
I haven't tested it too much, but I think it'll work for all general cases.
 
    
 
     
    