How can I group jQuery objects without using selectors?
Asked Answered
D

2

5

The jQuery.wrapAll function takes a dom subtree and wraps it around a jQuery object. I want to do that but without aquiring the target jQuery object via selector: I have a bunch of references to jQuery wrapped DOM elements and I want to apply wrapAll to all of them. Is it possible to do it without assigning a common class and selecting them through the class?

Depurative answered 17/1, 2012 at 18:0 Comment(2)
What's the problem? If you have the references, can't you just call myJqueryRef.wrapAll(...)?Mccourt
i have many references, not one reference. I want to combine themDepurative
C
8

You can add elements to a jQuery object using the add function. add accepts a selector (adds matching elements), a raw DOM element (adds it), an HTML fragment (creates the element(s) and adds them), and a jQuery object (adds all of the elements in it). That last probably matches what you're looking for.

Cannoneer answered 17/1, 2012 at 18:2 Comment(0)
P
5

Yes, add them all to the same object.

obj1.add(obj2).add(obj3).wrapAll('<div class="contentwrapper" />');
Pegasus answered 17/1, 2012 at 18:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.