Aggregating jQuery object?

I am creating a plugin and the plugin needs to do something like a collection of a collection of jQuery objects. How to do it?

For example:

<p><a>...</a></p>
<p><a>...</a></p>

      

FROM

(function( $ )
{
    $.fn.myfunc = function( settings )
    {

    };
})(jQuery);

      

In the context of a plugin called with $ ('p'). myfunc (), how would I return all elements for example? The items I return will not necessarily be contained or close to the selected items as this is just an example.

0


source to share


1 answer


jQuery also accepts an array, so you can create your own node stack and create a jQuery object from it.

Example:

(function( $ )
{
    $.fn.myfunc = function( settings )
    {
        var stack = [];
        stack.concat(this.find('a').toArray());
        stack.concat($('a.hot-links').toArray());
        return $($.unique(stack));
    };
})(jQuery);

      



Or simply:

return this.find('a'); // as return result of plugin

      

Also, take a look . pushStack () , which allows you to add elements to an already existing jQuery object.

+1


source







All Articles