.wrap()
S2C Home « Manipulation « .wrap()
DOM insertion around elements.
Description
The .wrap()
method is used to wrap a DOM element, HTML string, jQuery object or selector expression around each element in the matched set.
- Use the
.wrapAll()
method to wrap a DOM element, HTML string, jQuery object or selector expression around all elements in the matched set. - Use the
.wrapInner()
method to wrap a DOM element, HTML string, jQuery object or selector expression around the content of each element in the matched set. - The
.wrap()
method accepts any string or object that could be passed to the $() factory function to specify a DOM structure:- This structure may be nested several levels deep, but should contain only one innermost element.
- A copy of this structure will be wrapped around each element in the matched set.
- The
.wrap()
method returns the original matched set for chaining purposes. - When passing a HTML string, use well formed HTML with correctly closed tags.
Syntax
Signature | Description |
---|---|
.wrap( wrappingElement ) | A DOM element, HTML string, jQuery object or selector expression to wrap around each element in the matched set. |
.wrap( function(index) ) | A function that returns a DOM element, HTML string, jQuery object or selector expression to wrap around each element in the matched set. |
Parameters
Parameter | Description | Type |
---|---|---|
wrappingElement | A DOM element, HTML string, jQuery object or selector expression. | Element ,HTMLstring ,jQuery orSelector |
function(index) | A function.
|
Function |
Return
A jQuery
object containing the matched set before the call to the .wrap()
method.
.wrap( wrappingElement )
ExampleTop
Wrap a DOM element, HTML string, jQuery object or selector expression around each element in the matched set.
In the example below when the button is pressed the first time, we wrap 'div' 'p' and 'strong' elements around the saying below. The CSS for the 'div3' id and the html for the text we are wrapping is displayed first.
#main #div3 {
background-color: yellow;
padding: 10px;
border: 1px solid black;
}
...
<span id="saying">He who laughs last, laughs longest!</span>
He who laughs last, laughs longest!
$(function(){
$('#btn1').one('click', function() {
$('#saying').wrap('<div id="div3"><p><strong></strong></p></div>');
});
});
.wrap( function(index) )
ExampleTop
A function that returns a DOM element(s), HTML string, or jQuery object to wrap around each element in the matched set.
In the example below when the button is pressed the first time, we wrap 'div' 'p' and 'strong' elements around the sayings below. We append the index that is passed to the function to the 'div' id attribute so we end up with div ids of 'div0, 'div1' and 'div2' and get their different backgrounds for each saying. The CSS for 'div0, 'div1' and 'div2' and the html for the text we are wrapping is displayed first..
#main #div0, #main #div1, #main #div2 {
background-color: yellow;
padding: 10px;
border: 1px solid black;
}
#main #div1 {
background-color: orange;
}
#main #div2 {
background-color: teal;
}
...
<span class="saying2">A bird in the hand is worth two in the bush!</span><br />
<span class="saying2">Dont put all your eggs in one basket!</span><br />
<span class="saying2">The early bird catches the worm!</span><br />
$(function(){
$('#btn2').one('click', function() {
$('.saying2').wrap( function(index, html) {
return '<div id=' + '"div' + index + '"><p><strong></strong></p></div>';
});
});
});
Related Tutorials
jQuery Intermediate Tutorials - Lesson 4 - DOM Insertion, Around