jQuery insertBefore() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

.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)
CallWhat 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

ParameterDescription
Step 1Create or select the content (HTML string, existing nodes, or a jQuery object).
Step 2Choose the target: selector, element, HTML string, array, or jQuery object.
Step 3Call .insertBefore(target) — content becomes the previous sibling.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters 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"));
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.

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.

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.

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() Method — Replace Targets With Content

Learn how to replace target elements with a matched set.

replaceAll() Method — Replace Targets With Content tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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