jQuery .wrapInner() Method

Beginner
7 min read
Updated: Oct 2026
5 examples
Live preview

What Is .wrapInner()?

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

Remember
$("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
NeedUse
Wrap contents inside each$(els).wrapInner("<span>")
Parent around each match$(els).wrap("<div>")
One parent for the whole set$(els).wrapAll("<div>")
Remove a wrapper$(inner).unwrap() / contents unwrap

How to Use .wrapInner()

Select containers, then wrap what is inside each one.

  1. Select the outer elements whose contents you want wrapped.
  2. Pass HTML, a DOM element, a jQuery object, or a selector as the wrapper.
  3. Or pass a function (1.4+) that returns a wrapper per element.
  4. Use well-formed HTML with one innermost element for nested shells.
PieceRequired?Role
Matched setYesOuter elements (tags stay)
wrappingElementYes*HTML / element / jQuery / selector
function(index)Alt (1.4+)Returns wrapper; this = current element

Pseudocode

Pseudocode
for each matched element el at index i:
    if argument is a function:
        wrapper = function.call(el, i)
    else:
        wrapper = clone(wrappingElement)

    move all child nodes of el into the innermost wrapper
    append that wrapper as the only child of el

return original matched set

Cheat Sheet

Keep these facts handy while wrapping contents.

TopicDetail
SincejQuery 1.2 (callback 1.4)
ResultInner wrapper per matched element
Outer tagStays the same
vs wrap.wrap() adds a parent outside
vs wrapAll.wrapAll() = one shared outer parent
CallbackOnce per element with index
HTMLWell-formed; one innermost node

Live Preview

Wrap paragraph contents in <b>, or reset.

Browser demo of .wrapInner() — same idea as the examples below.

Hello

cruel

World

Result
Ready — pick an action and Run

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

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>.
  4. Outer. The <p> tag remains the parent.

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.
  4. Same idea. Equivalent to passing "<b>".

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.
  4. Apply. Contents move into the returned structure.

Example 4 — Nested Shell

Wrap contents in a nested structure with one innermost node.

jQuery
$("#box").wrapInner("<div><div></div></div>");
Try It Yourself

How It Works

  1. Parse. Nested HTML builds a small tree.
  2. Innermost. Contents land in the deepest empty element.
  3. Outer stays. #box remains the outermost node.
  4. Rule. Keep exactly one innermost container.

Example 5 — jQuery Object Template

Pass a jQuery object — it is cloned for each match.

jQuery
$("p").wrapInner($("<span class='mark'></span>"));
Try It Yourself

How It Works

  1. Build template. $("<span class='mark'>") is the pattern.
  2. Clone. Each paragraph gets its own copy.
  3. Wrap. Contents move into that span.
  4. Outer. Each p still wraps the new span.

Edge Cases & Pitfalls

Check these before wrapping many containers.

html

Use well-formed HTML

Official guidance: closed tags only — e.g. "<div></div>", not broken fragments.

nest

One innermost element

Nested wrappers are fine, but there must be a single deepest container for the contents.

empty

Empty elements still get a wrapper

An empty match receives an empty inner shell — useful or surprising depending on intent.

xss

Untrusted HTML is unsafe

Do not build wrapper strings from URL params, cookies, or form fields without escaping.

Browser Support

.wrapInner() has been part of jQuery since 1.2. The function callback arrived in 1.4 and runs once per matched element.

✓ Stable · jQuery 1.2+

jQuery .wrapInner()

Supported in all browsers that run jQuery 1.2+. Prefer a current jQuery 3.x build for modern projects.

100% jQuery core method
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.wrapInner() Universal

Bottom line: Use when the outer tag must stay and only the contents need a new shell.

Performance Notes

ApproachCostBest for
Few elementsCheapUI polish, emphasis tags
Callback per elementModerateDynamic class shells
Huge sets + deep nestsExpensiveAvoid — wrap fewer nodes

Each match gets its own wrapper. Prefer .wrapAll() when you only need one shared outer container for the set.

Key Takeaways

  • Role: wrap contents inside each match; outer tag stays.
  • vs wrap: .wrap() adds a parent outside.
  • Callback: once per element with index.
  • HTML: well-formed, one innermost container.

One line: .wrapInner() adds a shell around what is inside an element without replacing the outer tag.

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()

Wrap each matched element in its own outer parent structure.

.wrap() tutorial →

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