jQuery .wrapAll(): Put Selected Elements in One Wrapper

.wrapAll() places all selected elements inside one shared HTML structure. For elements attached to the document, the new wrapper takes the first selected element’s position. Unselected siblings stay under their existing parent.

.wrapAll() syntax and return value

The call below groups the two .item paragraphs in #items under a single div. Supply an HTML string, a selector, a DOM element or a jQuery object as the wrapper. The method returns the original jQuery selection, so a chained .addClass() applies to the selected elements.

$("#items .item").wrapAll('<div class="shared"></div>');

In a nested wrapper, provide one innermost element to hold the selection. The supported call forms are listed in jQuery API — .wrapAll().

Group two separated paragraphs in a runnable example

Save the complete code as wrap-all.html in UTF-8 and open it in a browser. The example needs internet access to load jQuery 4.0.0 from the official CDN. The script registers the button handlers once 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 .wrapAll() example</title>
  <style>
    body { margin: 24px; font: 1rem/1.6 system-ui, sans-serif; overflow-wrap: anywhere; }
    .actions { display: flex; flex-wrap: wrap; gap: .5rem; }
    button { font: inherit; }
    .shared { border: 2px solid #08716d; border-radius: .4rem; padding: 0 .75rem; }
  </style>
  <script src="https://code.jquery.com/jquery-4.0.0.min.js"></script>
</head>
<body>
  <main>
    <h1>jQuery .wrapAll() example</h1>
    <div class="actions">
      <button type="button" id="group">Group both paragraphs</button>
      <button type="button" id="undo">Remove the wrapper</button>
    </div>
    <div id="items">
      <p class="item" id="first">First paragraph</p>
      <p id="note">Middle note</p>
      <p class="item" id="second">Second paragraph</p>
    </div>
    <p id="output" role="status"></p>
  </main>
  <script>
    $(function () {
      const $items = $("#items .item");

      function report() {
        const inside = $("#items > .shared > .item").map(function () {
          return $(this).text();
        }).get().join(" / ");
        $("#output").text("Inside group: " + (inside || "—") +
          " | " + "Note parent: " + $("#note").parent().attr("id"));
      }

      $("#group").on("click", function () {
        if (!$("#items > .shared").length) {
          $items.wrapAll('<div class="shared"></div>');
        }
        report();
      });

      $("#undo").on("click", function () {
        $items.unwrap(".shared");
        report();
      });

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

The initial order is First paragraph, Middle note, Second paragraph. Click Group both paragraphs. A single border encloses First paragraph and Second paragraph. Middle note stays a direct child of #items and appears after the group.

Expand #items in the Elements panel. It contains .shared followed by #note, and .shared holds both .item paragraphs. The status line lists the paragraphs in order and reports items as the note’s parent.

Build the shared wrapper with a callback

Pass a callback to set an attribute when creating the wrapper. With a nonempty selection in jQuery 3.0 or later, it runs once, and this in a regular function is the first element. Before jQuery 3.0, the callback ran for each element and received its index.

$("#items > .item").wrapAll(function () {
  return $("<div>").addClass("shared").attr("data-first", this.id);
});

Reload the example before running the following code in the console. The single .shared wrapper gets data-first="first", using the first paragraph’s ID.

Check reading order and delegated events after moving elements

You can group elements from different parents. The method moves the existing elements, which can change selectors that depend on direct children and the order in which the page is read. If the groups need to stay in their original containers, limit each selection to one container.

Check delegated events if you attached a handler to an original ancestor. For instance, $oldParent.on("click", ".item", handler) receives a moved item’s clicks only while that item remains under $oldParent. After a move, you can delegate from an ancestor that still contains the items.

Use .wrap() to give each item an independent wrapper while keeping it next to its original siblings. Use .wrapAll() when the items should share one parent.

Prevent nested wrappers and remove the shared parent

Repeated .wrapAll() calls on the same selection add nested wrappers. The example skips grouping when #items > .shared already exists. If new items can appear later, decide whether to add them to that existing group or explicitly regroup the set.

The Remove the wrapper button passes ".shared" to .unwrap(). Both paragraphs stay together where the group was. Reload the example to restore the note between the paragraphs. To support a precise undo in an application, record each original position before moving the elements.

If nothing happens, check that $("#items .item").length is 2 and that jQuery loads before the example script. With an empty selection, the method creates no wrapper.