jQuery .wrap(): Wrap Each Element Separately
.wrap() places a separate HTML structure around each matched element. Selecting two elements normally creates two wrappers, unlike .wrapAll(), which creates one shared wrapper.
Syntax and behavior
$(".item").wrap("<div class='group'></div>");
$(".item").wrap("<div class='outer'><div class='inner'></div></div>");
Supply an HTML string, DOM element, jQuery object, or selector for the wrapper. If a selector or collection has several elements, jQuery uses the first. A nested wrapper should have one innermost insertion element. The return value is the original selection, not the new parents. A jQuery object is a collection that exposes jQuery methods for its selected elements.
The official jQuery .wrap() documentation describes additional call signatures and argument types.
Complete runnable example
Save the following code as a UTF-8 example.html file and open it in a browser. It loads the full jQuery 4.0.0 build from an external URL, so an internet connection is required. The $(function () { ... }) callback waits for the document to be ready before running the example.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery wrap example</title>
<style>
body { margin: 24px; font-family: sans-serif; line-height: 1.6; }
.marked { background: #fff0b3; border: 2px solid #835b00; }
.active { color: #a00000; font-weight: bold; }
button, input, select { font: inherit; }
#output { white-space: pre-wrap; overflow-wrap: anywhere; }
</style>
<script src="https://code.jquery.com/jquery-4.0.0.min.js"></script>
</head>
<body>
<div id="container">
<p class="item">First paragraph</p>
<p class="item">Second paragraph</p>
</div>
<p id="output" aria-live="polite"></p>
<script>
$(function () {
$(".item").wrap("<div class='group marked'></div>");
$("#output").text("Parents: " + $("#container > .group").length +
" / Paragraphs per parent: " + $(".group").map(function () { return $(this).children("p").length; }).get().join(", "));
});
</script>
</body>
</html>
Expected result
Each paragraph receives a separate border. The output is Parents: 2 / Paragraphs per parent: 1, 1. In the Elements panel, confirm that container has two group children, each containing one paragraph.
Important details and troubleshooting
- Running the wrapping code again adds another nested parent. Check for the intended existing wrapper before running it repeatedly on dynamic content.
- An id in the wrapper can be duplicated across several parents. Use a class for repeated structures.
- Choose a wrapper that can contain the selected elements in valid HTML. Avoid wrapping whole paragraphs in a span or another p.
- Use .wrapAll() to collect all targets into one parent. To remove the created parents, call .unwrap(".group") on their children.
Compare the behavior with .unwrap() and parent removal, .wrapAll() with one shared wrapper.










