Example 1 — HTML Wrapper
Wrap each paragraph’s contents in a <b>.
$("p").wrapInner("<b></b>"); How It Works
- Select. Every
pis matched. - Build. A
<b>is created for each match. - Move. Existing children move into that
<b>.

.wrapInner() wraps the contents inside each matched element (since jQuery 1.2). The outer tag stays put — unlike .wrap(), which adds a parent outside.
Start with these common calls. Keep them short and readable.
$("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 | Call | What it does |
|---|---|
Matched set | Outer elements (tags stay) |
wrappingElement | HTML / 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.
| Parameter | Description |
|---|---|
| index | Used in function(index). Returns wrapper; this = current element |
| Return Value | Description |
|---|---|
| jQuery object | Most setters and traversing helpers return the jQuery collection so you can chain more methods. |
Wrap each paragraph’s contents in a <b>.
$("p").wrapInner("<b></b>"); p is matched.<b> is created for each match.<b>.document.createElementPass a real DOM node as the inner wrapper template.
$("p").wrapInner(document.createElement("b")); b is built with the DOM API.Build a different wrapper per element from its text (since 1.4).
$(".inner").wrapInner(function () {
return "<div class='" + $(this).text() + "'></div>";
}); Wrap each matched element in its own outer parent structure.
CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
8 people found this page helpful