.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:
    1. This structure may be nested several levels deep, but should contain only one innermost element.
    2. A copy of this structure will be wrapped around each element in the matched set.
    3. The .wrap() method returns the original matched set for chaining purposes.
    4. 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
wrappingElementA DOM element, HTML string, jQuery object or selector expression.Element,
HTMLstring,
jQuery or
Selector
function(index)A function.
  • Each time the callback runs, it is passed the current index position of the element in the set.
  • The callback is fired in the context of the current element in the set, so the keyword this refers to that element.
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>'); 
  });
});

Press the button below to action the above code:


.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 />


A bird in the hand is worth two in the bush!
Dont put all your eggs in one basket!
The early bird catches the worm!


$(function(){
  $('#btn2').one('click', function() {
    $('.saying2').wrap( function(index, html) {
      return '<div id=' + '"div' + index + '"><p><strong></strong></p></div>';
    }); 
  });
});

Press the button below to action the above code: