.insertBefore(target) inserts every matched element immediately before the target as a sibling (since jQuery 1.0). Content comes first; the target is the argument — the reverse of .before().
Remember
$("p").insertBefore("#foo")
// same DOM result as:
$("#foo").before($("p"))
$("<span> Test </span>").insertBefore(".inner")
// sibling ABOVE target — not inside it
// (inside = .prependTo)
Build or select content, pass a target, insert as the previous sibling.
Create or select the content (HTML string, existing nodes, or a jQuery object).
Choose the target: selector, element, HTML string, array, or jQuery object.
Call .insertBefore(target) — content becomes the previous sibling.
Chain further on the returned inserted set (reliable since jQuery 1.9).
Piece
Required?
Role
jQuery collection
Yes
Content to insert
target
Yes
Where to place content (before this)
Return
—
Inserted element set (jQuery 1.9+)
Pseudocode
Pseudocode
content = matched elements (or new HTML nodes)
targets = resolve(target)
for each t in targets:
if first target:
move content before t
else:
clone content before t
return inserted set // original + clones
// Same idea as .before(), reversed syntax:
// content.insertBefore(target) ≡ target.before(content)
Reference
Cheat Sheet
Keep these facts handy while inserting siblings.
Topic
Detail
Syntax
.insertBefore(target)
Since
jQuery 1.0
Position
Previous sibling (outside the target)
vs .before()
Same result; content-first vs anchor-first
Callback?
No — build dynamic nodes first
One target
Existing nodes move
Many targets
Clones for targets after the first
Try it
Live Preview
Run the official patterns against a tiny demo tree and see the sibling order change.
Browser demo of .insertBefore() — same idea as the examples below.
Result
Ready — pick an action and run.
Code
jQuery Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Example 1 — Official Demo: Paragraphs Before #foo
Move every <p> so it sits before the element with id foo.
jQuery
// <div id="foo">FOO!</div><p>I would like to say: </p>
$("p").insertBefore("#foo");
// same as: $("#foo").before($("p"));
Select the heading.$("h2") already exists in the page.
One container. A single target means move, not clone.
New position. The h2 now sits above .container.
Return value. Since 1.9 you get the inserted set for chaining.
Example 4 — insertBefore vs before
Same sibling result; pick the syntax that matches how you think.
jQuery
// Content first — build, then place
$("<label>").text("Email").insertBefore("#email");
// Anchor first — same DOM result
$("#email").before($("<label>").text("Email"));
Build safely..text() sets label text without HTML injection.
Content-first.insertBefore reads naturally when the node is ready first.
Anchor-first.before reads naturally when the field is already selected.
Same sibling. Both place the label above the input, not inside it.
Edge Cases & Pitfalls
Check these before using .insertBefore() in real pages.
sibling
Sibling, not child
insertBefore places content outside the target. For first-child inside, use .prependTo().
move
One target moves nodes
Existing elements leave their old spot. Multiple targets clone after the first.
callback
No callback form
Unlike .before(), build dynamic content first, then call insertBefore.
xss
Untrusted HTML is unsafe
Prefer .text() for user strings. Never inject raw markup from URLs or forms.
Compatibility
Browser Support
.insertBefore() is a core jQuery method (since 1.0). It needs a browser DOM with jQuery loaded. Return-value chaining for single-target inserts is solid since 1.9.
✓ jQuery 1.0+ · Browsers only
jQuery .insertBefore()
Works in Chrome, Firefox, Safari, Edge, and other browsers with jQuery. Not for Node.js without a DOM.
100%Browser support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.insertBefore()Browsers
Bottom line: Use .insertBefore(target) in the browser for content-first sibling insertion. Prefer jQuery 1.9+ when chaining on the inserted set.
Analysis
Performance Notes
Approach
Cost
Best for
.insertBefore(one)
Low
Single sibling insert / move
.insertBefore(many)
Higher
Clones per extra target — expect more work
Native Node.insertBefore
Lowest
Hot paths without jQuery
Prefer one clear insert over many tiny DOM moves in a loop. Batch when you can, and avoid inserting untrusted HTML strings.
Remember
Key Takeaways
Role: insert matched content before a target as a sibling.
Syntax: content first — mirror of .before().
Move vs clone: one target moves; many targets clone.
One line:$(content).insertBefore(target) places siblings above the target — content-first twin of .before().
Frequently Asked Questions
.insertBefore(target) inserts every element in the matched set immediately before the target as a sibling. The target can be a selector, DOM element, HTML string, array, or jQuery object. It returns the inserted set. Available since jQuery 1.0.
Same DOM result, reversed syntax. Anchor first: $('#foo').before($('p')). Content first: $('p').insertBefore('#foo'). Prefer insertBefore when you build the content first, then choose where it goes.
.insertBefore() places content as the previous sibling (above). .insertAfter() places it as the next sibling (below). Both use content-first syntax.
.insertBefore() inserts outside the target as a sibling. .prependTo() inserts inside the target as the first child. Use insertBefore for labels and prefixes above a block; use prependTo to fill a container.
One target: existing nodes move (not clone). Multiple targets: the first gets the original; later targets get clones. The return set includes the original plus clones.
No. Unlike .before() (callback since 1.4), .insertBefore() only takes a target. Build dynamic nodes first — for example $('<label>').text('Name').insertBefore('#field') — then call insertBefore.