jQuery .wrap(): Wrap Each Element Separately

.wrap() adds a separate HTML structure around each selected element. If you select two paragraphs, each gets its own parent. Use .wrapAll() to put both paragraphs inside a shared parent.

.wrap() syntax and return value

The two calls below are separate alternatives. The first adds one level, and the second adds two. Run them on the original markup individually to compare the resulting structures.

$(".item").wrap('<div class="wrap-group"></div>');
$(".item").wrap('<div class="outer"><div class="inner"></div></div>');

Supply an HTML string, a DOM element, a jQuery object, or a selector. When a selector or jQuery collection matches several wrapper candidates, jQuery uses the first as a template. The returned jQuery object still contains the original selection. For example, a chained .addClass() changes the selected elements themselves.

For a nested wrapper, use a structure with a single innermost element to receive the selected element. In the second call, .inner becomes the immediate parent of each .item. See the supported argument forms in jQuery API — .wrap().

Run an example with one parent per paragraph

Save the complete code as wrap.html in UTF-8 and open it in a browser. The example needs an internet connection for the full jQuery 4.0.0 build from the official CDN. The code inside $(function () { ... }) runs when the document is ready.

<!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: 1rem/1.6 system-ui, sans-serif; }
    .actions { display: flex; flex-wrap: wrap; gap: .5rem; }
    button { font: inherit; }
    .item { margin: .5rem; }
    .wrap-group { margin: .5rem 0; border: 2px solid #08716d; border-radius: .4rem; }
    .is-wrapped { font-weight: 700; }
    #output { overflow-wrap: anywhere; }
  </style>
  <script src="https://code.jquery.com/jquery-4.0.0.min.js"></script>
</head>
<body>
  <main>
    <h1>jQuery .wrap() example</h1>
    <div class="actions">
      <button type="button" id="wrap">Wrap each paragraph</button>
      <button type="button" id="undo">Remove wrappers</button>
    </div>
    <div id="items">
      <p class="item">First paragraph</p>
      <p class="item">Second paragraph</p>
    </div>
    <p id="output" role="status"></p>
  </main>
  <script>
    $(function () {
      const $items = $("#items .item");

      function report() {
        const $wrappers = $("#items > .wrap-group");
        const counts = $wrappers.map(function () {
          return $(this).children(".item").length;
        }).get().join(", ");
        $("#output").text("Wrapper count: " + $wrappers.length +
          " / " + "Paragraphs per wrapper: " + (counts || "—"));
      }

      $("#wrap").on("click", function () {
        $items.filter(function () {
          return !$(this).parent().is(".wrap-group");
        }).wrap('<div class="wrap-group"></div>').addClass("is-wrapped");
        report();
      });

      $("#undo").on("click", function () {
        $items.unwrap(".wrap-group").removeClass("is-wrapped");
        report();
      });

      report();
    });
  </script>
</body>
</html>

Click Wrap each paragraph. Two borders appear, and the output reports two wrappers with one paragraph in each. Click it again to confirm that the wrapper count stays at two.

In the Elements panel, look for two .wrap-group children directly under #items. Each contains one paragraph with .item.is-wrapped. Click Remove wrappers to make both paragraphs direct children of #items again.

Build a different wrapper for each element

Pass a callback when each wrapper needs its own attribute, such as a sequence number. The index starts at zero within the matched set. In a regular function, this refers to the current paragraph.

$("#items .item").wrap(function (index) {
  return $("<div>").addClass("wrap-group").attr("data-index", index + 1);
});

Reload the example or remove the existing wrappers, then run the code below in the console. You get two parents with the same class and different attributes, data-index="1" and data-index="2".

Choose between .wrap(), .wrapAll(), and .wrapInner()

Method Result with two selected paragraphs
.wrap() Adds a separate parent outside each paragraph.
.wrapAll() Moves both paragraphs into one parent. If the selected elements were originally separated, grouping them at the first element can change their order relative to other content.
.wrapInner() Leaves both paragraphs in place and adds an inner wrapper around the contents of each. Use an inner span when styling the inline text inside each paragraph.

Choose .wrap() for individually framed items, .wrapAll() for one section containing the items, or .wrapInner() to style each item’s contents while keeping its original parent.

Prevent nesting and remove only the intended wrappers

Calling .wrap() repeatedly with the same selection adds more parent levels. The example uses .filter() to skip each paragraph whose immediate parent already has the .wrap-group class. Use a class on a repeated wrapper so each copy can coexist without duplicate IDs.

.unwrap(".wrap-group") removes only an immediate parent that matches the class. If that parent contains other children, they move up a level as well. Apply it to wrappers dedicated to this job. The optional parent selector is described in jQuery API — .unwrap().

If nothing changes, check that $("#items .item").length is 2. If you see $ is not defined, check that the jQuery file loads before the example script. To wrap whole paragraphs, use a container such as a div that can contain them.

For more complete examples of grouping or removing wrappers, see .wrapAll() and .unwrap().