jQuery API

.wrapInner()

.wrapInner( wrappingElement ) Returns: jQuery

Description: Wrap an HTML structure around the content of each element in the set of matched elements.

  • version added: 1.2.wrapInner( wrappingElement )

    wrappingElementAn HTML snippet, selector expression, jQuery object, or DOM element specifying the structure to wrap around the content of the matched elements.

  • version added: 1.4.wrapInner( function(index) )

    function(index)A callback function which generates a structure to wrap around the content of the matched elements. Receives the index position of the element in the set as an argument. Within the function, this refers to the current element in the set.

The .wrapInner() function can take 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 inmost element. The structure will be wrapped around the content of each of the elements in the set of matched elements.

Consider the following HTML:

<div class="container">
  <div class="inner">Hello</div>
  <div class="inner">Goodbye</div>
</div>

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

$('.inner').wrapInner('<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 the content of each matched element:

<div class="container">
  <div class="inner">
    <div class="new">Hello</div>
  </div>
  <div class="inner">
    <div class="new">Goodbye</div>
  </div>
</div>

The second version of this method allows us to instead specify a callback function. This callback function will be called once for every matched element; it should return a DOM element, jQuery object, or HTML snippet in which to wrap the content of the corresponding element. For example:

$('.inner').wrapInner(function() {
  return '<div class="' + this.nodeValue + '" />';
});

This will cause each <div> to have a class corresponding to the text it wraps:

<div class="container">
  <div class="inner">
    <div class="Hello">Hello</div>
  </div>
  <div class="inner">
    <div class="Goodbye">Goodbye</div>
  </div>
</div>

Note: When passing a selector string to the .wrapInner() function, the expected input is well formed HTML with correctly closed tags. Examples of valid input include:

$(elem).wrapInner("<div class='test' />");
$(elem).wrapInner("<div class='test'></div>");
$(elem).wrapInner("<div class=\"test\"></div>");

Examples:

Example: Selects all paragraphs and wraps a bold tag around each of its contents.

<!DOCTYPE html>
<html>
<head>
  <style>p { background:#bbf; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>Hello</p>

  <p>cruel</p>
  <p>World</p>
<script>$("p").wrapInner("<b></b>");</script>

</body>
</html>

Demo:

Example: Wraps a newly created tree of objects around the inside of the body.

<!DOCTYPE html>
<html>
<head>
  <style>

  div { border:2px green solid; margin:2px; padding:2px; }
  p { background:yellow; margin:2px; padding:2px; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  Plain old text, or is it?
<script>$("body").wrapInner("<div><div><p><em><b></b></em></p></div></div>");</script>

</body>
</html>

Demo:

Example: Selects all paragraphs and wraps a bold tag around each of its contents.

<!DOCTYPE html>
<html>
<head>
  <style>p { background:#9f9; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>Hello</p>

  <p>cruel</p>
  <p>World</p>
<script>$("p").wrapInner(document.createElement("b"));</script>

</body>
</html>

Demo:

Example: Selects all paragraphs and wraps a jQuery object around each of its contents.

<!DOCTYPE html>
<html>
<head>
  <style>

  p { background:#9f9; }
  .red { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>Hello</p>
  <p>cruel</p>
  <p>World</p>
<script>$("p").wrapInner($("<span class='red'></span>"));</script>

</body>
</html>

Demo:

Support and Contributions

Need help with .wrapInner() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .wrapInner()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • http://www.unleashedmind.com/ sun

    A clarification on the difference to .wrap() would be great to see here; ideally, in the initial description already, possibly enhanced by a code example.

    • http://twitter.com/michl86 michl86

      wrap calls wrapAll for each result in the selector

      wrapInner instead, selects the content of all children definied by the selector and then calls wrapAll for each of them.

      use wrap or wrapAll if you would create elements higher in the hierachie, and wrapInner for elements deeper in your hierachie.

  • http://profiles.yahoo.com/u/WWQJW2KBHZOK2ZVFABDMPMVDLM NickT

    Here's an example I like – I have a table with rows of test builds and test run times (in hours) in column 4. To make it easier to compare by build, I've added a simple bar graph to each time value:

    $("#tClientTestBuilds td:nth-child(4)").wrapInner(function() {
    return "<div style='background-color:#2C3D5A;color:white;width:" +
    5 * this.nodeValue + "px'> </div>";
    });

  • vinayc

    What will be the returned set of this function? Will it be a original set or a set of newly created elements (those with which inner contents has been wrapped)?

  • Tinacart

    why is there no unwrapInner() ????

  • Tinacart

    why is there no unwrapInner() ????