.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().
Syntax
Start with these common calls. Keep them short and readable.
jQuery
$("p").insertBefore("#foo")
// same DOM result as:
$("#foo").before($("p"))
$("<span> Test </span>").insertBefore(".inner")
// sibling ABOVE target — not inside it
// (inside = .prependTo)
Call
What it does
insertBefore()
Learn jQuery .insertBefore(): insert matched elements before a target as siblings, content-first syntax vs .before(), move vs clone, examples, pitfalls, and FAQs.
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Create or select the content (HTML string, existing nodes, or a jQuery object).
Step 2
Choose the target: selector, element, HTML string, array, or jQuery object.
Step 3
Call .insertBefore(target) — content becomes the previous sibling.
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.
jQuery Examples
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.
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.