jQuery .wrapInner() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

.wrapInner() wraps the contents inside each matched element (since jQuery 1.2). The outer tag stays put — unlike .wrap(), which adds a parent outside.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("p").wrapInner("<b></b>")
$(".inner").wrapInner(document.createElement("div"))
$(".inner").wrapInner(function () {
  return "<div class='" + $(this).text() + "'>";
});

Before:  <p>Hello</p>
After:   <p><b>Hello</b></p>   // outer <p> unchanged
CallWhat it does
Matched setOuter elements (tags stay)
wrappingElementHTML / element / jQuery / selector
function(index)Returns wrapper; this = current element

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
indexUsed in function(index). Returns wrapper; this = current element

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

Example 1 — HTML Wrapper

Wrap each paragraph’s contents in a <b>.

jQuery
$("p").wrapInner("<b></b>");
Try It Yourself

How It Works

  1. Select. Every p is matched.
  2. Build. A <b> is created for each match.
  3. Move. Existing children move into that <b>.

Example 2 — document.createElement

Pass a real DOM node as the inner wrapper template.

jQuery
$("p").wrapInner(document.createElement("b"));
Try It Yourself

How It Works

  1. Create. A detached b is built with the DOM API.
  2. Pass. That node is the wrapping element.
  3. Clone/use. jQuery wraps each paragraph’s contents.

Example 3 — Callback Form

Build a different wrapper per element from its text (since 1.4).

jQuery
$(".inner").wrapInner(function () {
  return "<div class='" + $(this).text() + "'></div>";
});
Try It Yourself

How It Works

  1. Per element. The function runs once for each match.
  2. this. Is the current DOM element.
  3. Return. HTML (or jQuery/DOM) becomes that element’s inner wrapper.

Frequently Asked Questions

.wrapInner() wraps the contents inside each matched element. The matched element tag stays as the outer node; children move into a new inner wrapper. Returns the original matched set. Available since jQuery 1.2; callback form since 1.4.
.wrap() adds a new parent outside the matched element. .wrapInner() keeps the matched element and wraps what is inside it.
.wrapInner() wraps contents inside each match separately. .wrapAll() wraps the whole set of matched elements in one shared outer parent.
Yes, since 1.4. The function runs once per matched element with the index argument; this is the current element. Return HTML or a jQuery/DOM structure.
No. Templates are cloned for each match. The original node stays where it is.
Select the inner wrapper and unwrap its contents, for example $('p').children('b').contents().unwrap() or unwrap the inner element itself when appropriate.
Use well-formed HTML with correctly closed tags, such as '<div></div>' or '<div><p></p></div>' with a single innermost element.

Next: .wrap() Method

Wrap each matched element in its own outer parent structure.

.wrap() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful