jQuery insertBefore() Method

Beginner
6 min read
Updated: Oct 2026
4 examples
Live preview

What Is insertBefore()?

.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)
NeedUse
Content first, before target$(el).insertBefore(target)
Anchor first$(target).before(content)
After target (sibling).insertAfter()
Inside as first child.prependTo()

How to Use insertBefore()

Build or select content, pass a target, insert as the previous sibling.

  1. Create or select the content (HTML string, existing nodes, or a jQuery object).
  2. Choose the target: selector, element, HTML string, array, or jQuery object.
  3. Call .insertBefore(target) — content becomes the previous sibling.
  4. Chain further on the returned inserted set (reliable since jQuery 1.9).
PieceRequired?Role
jQuery collectionYesContent to insert
targetYesWhere 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)

Cheat Sheet

Keep these facts handy while inserting siblings.

TopicDetail
Syntax.insertBefore(target)
SincejQuery 1.0
PositionPrevious sibling (outside the target)
vs .before()Same result; content-first vs anchor-first
Callback?No — build dynamic nodes first
One targetExisting nodes move
Many targetsClones for targets after the first

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.

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"));
Try It Yourself

How It Works

  1. Select content. $("p") is what will move.
  2. Pass the target. "#foo" is the anchor.
  3. Insert before. The paragraph becomes the previous sibling of #foo.
  4. Single target. The node moves — it is not cloned.

Example 2 — Official Demo: HTML Before .inner

Create a span and insert it before every .inner element.

jQuery
// <div class="container">
//   <h2>Greetings</h2>
//   <div class="inner">Hello</div>
//   <div class="inner">Goodbye</div>
// </div>
$("<span> Test </span>").insertBefore(".inner");
Try It Yourself

How It Works

  1. Parse HTML. $("<span>…") builds a new node.
  2. Match targets. Both .inner divs resolve.
  3. Clone for extras. First target gets the original; the second gets a clone.
  4. Sibling only. Spans sit beside .inner, not inside.

Example 3 — Official Demo: Move <h2>

Relocate an existing heading so it precedes a container.

jQuery
// Start: h2 lives inside .container
$("h2").insertBefore($(".container"));
// Result: h2 moves out and sits before .container
Try It Yourself

How It Works

  1. Select the heading. $("h2") already exists in the page.
  2. One container. A single target means move, not clone.
  3. New position. The h2 now sits above .container.
  4. 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"));
Try It Yourself

How It Works

  1. Build safely. .text() sets label text without HTML injection.
  2. Content-first. insertBefore reads naturally when the node is ready first.
  3. Anchor-first. before reads naturally when the field is already selected.
  4. 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.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All 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.

Performance Notes

ApproachCostBest for
.insertBefore(one)LowSingle sibling insert / move
.insertBefore(many)HigherClones per extra target — expect more work
Native Node.insertBeforeLowestHot 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.

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.
  • Inside vs outside: first child → .prependTo().

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.

Next: replaceAll()

Learn how to replace target elements with a matched set.

replaceAll() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful