tadam logo
Did you find an error in the text?
Select this with mouse and press
Ctrl + Enter
Xhtml.co.il Check Spelling
Orphus system

wrapInner()

.wrapInner( wrappingElement )

Returns: jQuery

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

.wrapInner( wrappingElement )

version added: 1.2
wrappingElement
An HTML snippet, selector expression, jQuery object, or DOM element specifying the structure to wrap around the content of the matched elements.

.wrapInner( wrappingFunction )

version added: 1.4
wrappingFunction
A callback function which generates a structure to wrap around the content of the matched elements.

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>

Examples

Example 1

Selects all paragraphs and wraps a bold tag around each of its contents.
$("p").wrapInner("<b></b>");
The output of the code above will be:

Example 1 - Full source:

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>  
  <script>
  $(document).ready(function(){
    $("p").wrapInner("<b></b>");
  });
  </script>
  <style>p { background:#bbf; }</style>
</head>
<body>
  <p>Hello</p>
  <p>cruel</p>
  <p>World</p>
</body>
</html>

Example 2

Selects all paragraphs and wraps a bold tag around each of its contents.
$("p").wrapInner(document.createElement("b"));
The output of the code above will be:

Example 2 - Full source:

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>  
  <script>
  $(document).ready(function(){
    $("p").wrapInner(document.createElement("b"));
  });
  </script>
  <style>p { background:#9f9; }</style>
</head>
<body>
  <p>Hello</p>
  <p>cruel</p>
  <p>World</p>
</body>
</html>

Example 3

Wraps a newly created tree of objects around the inside of the body.
("body").wrapInner("<div><div><p><em><b></b></em></p></div></div>");
The output of the code above will be:

Example 3 - Full source:

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

Example 4

Selects all paragraphs and wraps a jQuery object around each of its contents.
$("p").wrapInner($("<span class='red'></span>"));
The output of the code above will be:

Example 4 - Full source:

<!DOCTYPE html>
<html>
<head>
  <style>
  p { background:#9f9; }
  .red { color:red; }
  </style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript" language="javascript">
     $(document).ready(function(){
$("p").wrapInner($("<span class='red'></span>"));
     });
  </script>
</head>
<body>
  <p>Hello</p>
  <p>cruel</p>
  <p>World</p>
</body>
</html>
Was this information helpful?
   

Comments