jQuery .insertBefore(): Insert Content as the Previous Sibling

jQuery .insertBefore() puts the selected content immediately before each target. The target and the inserted elements share a parent. Use it to move a notice before a schedule heading or to add content before a particular item.

Insert a sibling immediately before a target

$(content).insertBefore(target);

Start with the elements you want to insert. Pass a selector, a DOM element, or a jQuery object as target. The Document Object Model (DOM) represents the document as a tree of nodes. The returned collection contains the inserted elements, so a chained .addClass() styles them.

Official jQuery .insertBefore() documentation

Move a notice and test its button

Save the code in UTF-8 as insert-before-example.html and open it in a browser. It loads jQuery 4.0.0 from a CDN and needs an internet connection. Try the button inside the notice before and after moving it.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Move a notice before a schedule heading</title>
  <style>
    body { margin: 1rem; font: 1rem/1.5 system-ui, sans-serif; overflow-wrap: anywhere; }
    main { max-width: 42rem; margin-inline: auto; }
    section { margin-block: .75rem; border: 1px solid #9eb1bf; border-radius: .4rem; padding: .75rem 1rem; }
    section h2 { font-size: 1.1rem; margin-top: 0; }
    button { font: inherit; margin: .15rem; padding: .35rem .55rem; }
    #notice { border: 1px solid #847045; padding: .45rem; }
    .highlight { background: #fff0ba; color: #3c2a06; }
  </style>
  <script src="https://code.jquery.com/jquery-4.0.0.min.js"></script>
</head>
<body>
  <main>
    <h1>Move a notice before a schedule heading</h1>
    <button id="move" type="button">Move above the schedule</button>
    <button id="restore" type="button">Return to drafts</button>
    <section id="draft" aria-labelledby="draft-title">
      <h2 id="draft-title">Drafts</h2>
      <div id="notice">
        <p>Opening hours change this week.</p>
        <button id="confirm" type="button">Acknowledge notice</button>
        <p id="count" role="status">Acknowledgments: 0</p>
      </div>
    </section>
    <section id="schedule" aria-labelledby="schedule-title">
      <h2 id="schedule-title">Schedule</h2>
      <p>Registration opens at 9 a.m.</p>
    </section>
  </main>
  <script>
    $(function () {
      const $notice = $("#notice");
      let count = 0;
      $("#confirm").on("click", function () {
        count += 1;
        $("#count").text("Acknowledgments: " + count);
      });
      $("#move").on("click", function () {
        $notice.insertBefore("#schedule-title").addClass("highlight");
      });
      $("#restore").on("click", function () {
        $notice.appendTo("#draft").removeClass("highlight");
      });
    });
  </script>
</body>
</html>
  1. Click Acknowledge notice in the draft section. The counter changes from 0 to 1.
  2. Click Move above the schedule. The notice moves into the schedule section immediately before its heading. Click the notice’s button again and the count becomes 2.
  3. Click Return to drafts to restore its original location. Reload the page to reset both its location and its count.

The example has one destination, so the original #notice and its button move together. The click handler remains attached to the same button node, so it still runs without being registered again.

Distinguish a preceding sibling from a first child

Method Insertion position Returned elements
.insertBefore() Immediately before the target, under the same parent Inserted elements
.before() Immediately before the target, under the same parent Target elements
.insertAfter() Immediately after the target, under the same parent Inserted elements
.prependTo() At the beginning inside the target Inserted elements

$(target).before(content) starts with the target, while $(content).insertBefore(target) starts with the content. Both insert in the same position. Choose based on which elements you want to work with in the next call.

Official jQuery .before() documentation

Run this snippet in the example page’s console. It constructs a temporary parent and checks the sibling before the heading and the first child of the parent.

const $group = $("<div>");
const $target = $("<h2>").appendTo($group);
const $sibling = $("<p>").text("Sibling before the heading").insertBefore($target);
const $child = $("<p>").text("First child of the parent").prependTo($group);
console.log($target[0].previousElementSibling === $sibling[0]);
console.log($group[0].firstElementChild === $child[0]);

Both lines print true. $sibling immediately precedes the heading. $child is the first child inside the parent.

Insert in several places or add new text

With more than one target, the original goes before the last target and copies go before the others. To keep the original in place, insert a copy made with .clone(). Give copied elements and their descendants unique IDs or remove their copied IDs.

To display a string from a form or API, set it on an empty element with .text(). Run this snippet in the example’s console to display <Extra notice> immediately before the schedule heading with its angle brackets. Reload to clear it.

const message = "<Extra notice>";
$("<p>").text(message).insertBefore("#schedule-title");

How jQuery .text() handles strings

Check the insertion result

  • Check the selector’s .length and whether the target has a parent. The target needs a parent to receive a sibling.
  • In the Elements panel, confirm that the new element and the target share a parent and that the new element immediately precedes it.
  • A selector that matches several headings inserts content before each one. Narrow it if you only want one insertion.

Insert after the target with .insertAfter() · Insert at the start inside a target with .prependTo() · Start with the target in .before()