How can I group jQuery objects without using selectors?

The jQuery.wrapAll function takes a dom subtree and wraps it around a jQuery object. I want to do this, but without accessing the jQuery target with a selector: I have a bunch of links to elements placed in jQuery and I want to apply wrapAll to all of them. Can this be done without assigning a general class and selecting them through the class?

+5
source share
2 answers

You can add elements to a jQuery object using a function add. addaccepts a selector (adds matching elements), a raw DOM element (adds it), an HTML snippet (creates an element and adds them), and a jQuery object (adds all elements in it). The latter probably matches what you are looking for.

+8
source

Yes, add them all to the same object.

obj1.add(obj2).add(obj3).wrapAll('<div class="contentwrapper" />');
+5
source

All Articles