A repository of over 1000 quality jQuery plugins

jQuery .wrapAll()

Learn all about the jQuery function .wrapAll().

The .wrapAll() function can take any string or object that could be passed to the $() function to specify a DOM structure. This structure may be nested several levels deep, but should contain only one inmost element. The structure will be wrapped around all of the elements in the set of matched elements, as a single group.

Consider the following HTML:

1
2
3
4
<div class="container">
<div class="inner">Hello</div>
<div class="inner">Goodbye</div>
</div>

Using .wrapAll(), we can insert an HTML structure around the inner <div> elements like so:

1
$( ".inner" ).wrapAll( "<div class='new' />");

The new <div> element is created on the fly and added to the DOM. The result is a new <div> wrapped around all matched elements:

1
2
3
4
5
6
<div class="container">
<div class="new">
<div class="inner">Hello</div>
<div class="inner">Goodbye</div>
</div>
</div>