jQuery .before(): Insert Content as the Previous Sibling
jQuery .before() inserts content immediately before each selected target as a sibling under the same parent. Use .before(function) when each target needs its own message, such as a delivery note for each order summary.
Insert content immediately before a target
$(target).before(content, moreContent);
Select the target first. Pass an HTML string, a DOM element, a text node, a jQuery object, or an array of nodes. Several arguments in one call are inserted in the supplied order. The Document Object Model (DOM) represents a document as a tree of nodes. The method returns the target collection.
Official jQuery .before() documentation
Add a different delivery note to each order
Save the code as before-example.html in UTF-8 and open it in a browser. It loads jQuery 4.0.0 from a CDN and needs internet access. Click Show delivery notes to compare the two orders.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Show delivery notes before order summaries</title>
<style>
body { margin: 1rem; font: 1rem/1.5 system-ui, sans-serif; overflow-wrap: anywhere; }
main { max-width: 38rem; margin-inline: auto; }
button { font: inherit; margin: .2rem; padding: .4rem .6rem; }
.summary { padding: .6rem; border: 1px solid #a0b1bc; border-radius: .35rem; }
.has-note { border-color: #42698a; background: #edf4fa; }
.delivery-note { color: #28504b; background: #e4f4eb; padding: .35rem .5rem; }
</style>
<script src="https://code.jquery.com/jquery-4.0.0.min.js"></script>
</head>
<body>
<main>
<h1>Show delivery notes before order summaries</h1>
<button id="show" type="button">Show delivery notes</button>
<button id="clear" type="button">Clear delivery notes</button>
<div id="orders">
<p class="summary" data-day="Expected Monday">Order A · Notebook</p>
<p class="summary" data-day="Expected Wednesday">Order B · Pen</p>
</div>
</main>
<script>
$(function () {
const $summaries = $("#orders .summary");
function clear() {
$("#orders .delivery-note").remove();
$summaries.removeClass("has-note");
}
$("#show").on("click", function () {
clear();
$summaries.before(function (index, previousHtml) {
const text = (index + 1) + ". " + this.dataset.day;
return $("<p>").addClass("delivery-note").text(text);
}).addClass("has-note");
});
$("#clear").on("click", clear);
});
</script>
</body>
</html>
- The first order gets
1. Expected Mondayabove it. The second gets2. Expected Wednesday. - Each note shares its parent with the corresponding order. The chained
.addClass('has-note')styles the order summaries, so the notes and summaries get different backgrounds. - On another click, the example removes its old notes and creates new ones. Clear delivery notes removes the notes and the highlighting.
jQuery calls the function once per target. index starts at zero, and previousHtml is the target’s inner HTML before insertion. In a regular function, this is the current target. The example reads each order’s note from this.dataset.day.
Move an existing node or insert it in several places
Pass an existing paragraph to a single target and it moves from its old parent to the position immediately before the target. Run the snippet in the example’s console. It prints true 0 and true to show the returned object is the target, the source is empty, and the preceding sibling is the original.
const $source = $("<div>");
const $group = $("<div>");
const $notice = $("<p>").text("Notice to move").appendTo($source);
const $target = $("<p>").text("Target paragraph").appendTo($group);
const returned = $target.before($notice);
console.log(returned === $target, $source.children().length);
console.log($target[0].previousElementSibling === $notice[0]);
If you insert the same node before several targets, the original goes before the last target and copies go before the others. Pass a copy made with .clone() to keep the original in place. Check copied elements and their descendants for duplicate IDs.
Compare related insertion methods
| Method | Insertion position | Returned elements |
|---|---|---|
.before() |
Immediately before the target, under the same parent | Target elements |
.insertBefore() |
Immediately before the target, under the same parent | Inserted elements |
.after() |
Immediately after the target, under the same parent | Target elements |
.prepend() |
At the beginning inside the target | Target elements |
.before() and .insertBefore() insert at the same position. Start with the target and use .before() to keep working on the target. Start with the content and use .insertBefore() to work on the new nodes.
Official jQuery .insertBefore() documentation
Insert text and check the result
Use an empty element and .text() for strings from forms or APIs. Run this snippet in the example’s console. <Extra delivery note> appears before the first order with its angle brackets visible. Reloading clears this practice paragraph.
const label = "<Extra delivery note>";
$("#orders .summary").first().before($("<p>").text(label));
- If nothing appears, check the selection’s
.lengthand whether the target has a parent. A target needs a parent to receive a sibling. - In the Elements panel, check that the note and order share a parent and are next to each other.
- If notes accumulate, remove only the ones you previously added or update their text.
Start with the content in .insertBefore() · Insert at the start inside a target with .prepend() · Insert immediately after a target with .after()




