.before(content) inserts content as the previous sibling of each matched element (since jQuery 1.0). It sits outside the element — use .prepend() to insert inside.
Syntax
Start with these common calls. Keep them short and readable.
jQuery
$("p").before("<b>Hello</b>") // anchor first
$("<b>Hello</b>").insertBefore("p") // content first (same result)
$("p").before(" Hello ") // HTML / text
$("p").before(document.createTextNode("Hi"))
$(".box").before($("h2")) // move existing node
Call
What it does
Matched set
Anchor element(s) — content goes before them
content
HTML, Element, Text, Array, or jQuery (*or callback)
Callback (1.4+)
function(index); since 1.10 also (index, html)
Return (1.9+)
Original matched set (unchanged)
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
14+
Used in Callback (1.4+). function(index); since 1.10 also (index, html)
19+
Used in Return (1.9+). Original matched set (unchanged)
Return Value
Return Value
Description
jQuery object
Most setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefined
Getters usually return a value from the first matched element, or undefined when nothing applies.
<b> removed from original location
Inserted as previous sibling of <p>
One target → move; many targets → clone extras
How It Works
Select content.$("b") points at an existing bold element.
One target. jQuery detaches it and inserts it before the paragraph.
Many targets. Clones fill every match except the last.
Frequently Asked Questions
.before() inserts content immediately before each matched element as a previous sibling — outside the element, not inside it. It accepts HTML strings, DOM elements, text nodes, arrays, jQuery objects, or a callback (since 1.4). Returns the original matched set for chaining. Available since jQuery 1.0.
.before() inserts as the previous sibling. .after() inserts as the next sibling. Both operate outside the matched element.
Same job, reversed syntax. With .before(), the anchor comes first: $('p').before('<b>Hi</b>'). With .insertBefore(), the content comes first: $('<b>Hi</b>').insertBefore('p').
.before() adds a sibling above the element. .prepend() adds a child inside the element as the first child.
With one matched target, jQuery moves the inserted element. With multiple targets, it clones for every match except the last — the original moves to the final target.
Since jQuery 1.4, yes. .before(function(index)) runs once per match. Since 1.10, the callback can also receive the old HTML: function(index, html). Return content to insert; this is the current element.
As of jQuery 1.9, .before() on a node without a parent has no effect and still returns the original set.