The .insertBefore() method inserts every matched element immediately before a target as a sibling — content-first syntax, the mirror of .before(). This tutorial covers selectors, DOM elements, HTML strings, move vs clone behavior, comparisons with .before(), .insertAfter(), and .prependTo(), the official jQuery demos, and five practical try-it examples.
01
.insertBefore()
Content first
02
Target
Selector
03
Move
DOM nodes
04
vs .before()
Reversed
05
Sibling
Not child
06
Since 1.0
Core API
Fundamentals
Introduction
When you build new markup and already know what it is but not yet where it goes, .insertBefore() lets you configure the element first, then choose the anchor. Available since jQuery 1.0, it is the content-first counterpart to .before().
Pass a CSS selector, DOM element, HTML string, array, or jQuery object as the target parameter. The matched collection is inserted as the previous sibling before each resolved target. Unlike .before(), there is no callback form — build dynamic nodes first, then call .insertBefore().
.insertBefore() and .before() perform the same DOM task with reversed syntax. Use .prependTo() when content belongs inside the target. Browse the jQuery DOM hub for related manipulation methods.
Concept
Understanding the .insertBefore() Method
.insertBefore( target ) takes every element in the current jQuery collection and inserts it immediately before the specified target element(s). The target stays in place; matched elements become its preceding siblings. Since jQuery 1.9, the return value is the inserted element set — not the original collection.
When moving an existing node before a single target, jQuery moves it rather than cloning. With multiple target elements, clones are created for every target after the first.
💡
Beginner Tip
$("<span> Test </span>").insertBefore(".inner") adds a span before every .inner element — the official jQuery demo. Same result as $(".inner").before("<span> Test </span>") with reversed syntax.
Foundation
📝 Syntax
jQuery .insertBefore() accepts one argument:
Insert before target — since 1.0
jQuery
.insertBefore( target )
target — selector, DOM element, HTML string, array, or jQuery object.
Matched elements insert as the previous sibling before each resolved target.
Returns the inserted element collection (since jQuery 1.9 for reliable chaining).
📋 .insertBefore() vs .before() vs .insertAfter() vs .prependTo()
Four related insertion APIs — pick the right position and syntax.
.insertBefore()
content first
Insert matched elements before target — content precedes method: $(el).insertBefore(target)
.before()
target first
Insert content before matched elements — anchor precedes method: $(target).before(content)
.insertAfter()
next sibling
Insert after target — content first: $(el).insertAfter(target)
.prependTo()
first child
Insert inside target as first child — not a sibling operation
Hands-On
Examples Gallery
Examples 1–3 follow the official jQuery API documentation. Examples 4–5 cover build-then-insert chains and multi-target insertion. Use DevTools or the Try-it links to run each snippet in the browser.
📚 Getting Started
Official jQuery demos and reversed-syntax sibling insertion.
Example 1 — Official Demo: Insert Paragraphs Before #foo
Move every <p> to precede the element with id foo.
Before: <div id="foo">FOO!</div> <p>I would like to say...</p>
After: <p>I would like to say...</p> <div id="foo">FOO!</div>
Paragraph moves before #foo as sibling
How It Works
The matched collection (p elements) is inserted before #foo. The paragraph disappears from its original position and becomes a preceding sibling of the target. Returns the inserted element set since jQuery 1.9.
Example 2 — Official Demo: Insert HTML Before .inner
Create markup and insert it before every .inner element — content-first syntax.
jQuery
$( "<span> Test </span>" ).insertBefore( ".inner" );
Each .inner gets <span> Test </span> as previous sibling
Official jQuery insertBefore HTML pattern
Equivalent to $(".inner").before("<span> Test </span>")
How It Works
jQuery parses the HTML string into a new element, then inserts it before each resolved .inner target. With multiple targets, clones are created for every target after the first.
📈 Practical Patterns
Moving elements, build-then-insert chains, and multi-target insertion.
Example 3 — Official Demo: Move <h2> Before a Container
Relocate an existing heading to precede a layout container — jQuery moves it from its original location.
h2 removed from original location
Inserted as sibling before .container
Single target → element moves (not cloned)
How It Works
When the target is a single element, jQuery moves the matched heading rather than cloning it. The return value is the inserted element set — useful for further chaining on the moved node.
Example 4 — Build Then Insert: Label Before Field
Create a form label, configure it, then insert before the field wrapper — content-first reads naturally.
<label>Email address</label> inserted before #email-wrap
.text() keeps label text safe from HTML injection
Chain configuration before choosing destination
How It Works
Because .insertBefore() has no callback, build dynamic siblings first. Chain .text(), .addClass(), or event handlers on the new node, then call .insertBefore() at the end.
Example 5 — Insert Before Multiple Targets
Insert a required marker before every field wrapper — jQuery clones for all but the first target.
jQuery
$( "<span class='req'>*</span>" ).insertBefore( ".field-wrap" );
// Multiple .field-wrap → marker cloned before each (except first uses original)
Marker appears before each .field-wrap
Multiple targets → clones for targets after the first
Return value includes original + clones
How It Works
When the target selector matches multiple elements, jQuery inserts before each one. The first target receives the original node; subsequent targets get clones. This differs from calling .before() on multiple anchors with one content argument.
Applications
🚀 Common Use Cases
Build-then-place — create labels with $('<label>'), configure them, then .insertBefore() the field.
Form labels — insert accessible labels as siblings above field wrappers.
Section headings — place an <h2> before a content block.
Relocate headings — move an existing <h2> to precede a layout container.
Required markers — insert * spans before multiple field wrappers.
Reversed syntax — prefer insertBefore when content is built before the destination is chosen.
🧠 How .insertBefore() Inserts Content
1
Match content to insert
jQuery collection holds the element(s) that will be placed before the target.
Content
2
Resolve target element(s)
Selector, DOM node, or jQuery object passed as the insertBefore argument.
Target
3
Insert as previous sibling
Matched elements placed immediately before each resolved target.
Before
4
⬆
Return inserted set
Inserted element collection since jQuery 1.9 — chain further on the new nodes.
Important
📝 Notes
Available since jQuery 1.0 — no callback form (build content first).
Inserts as the previous sibling before the target; use .insertAfter() for the next sibling.
Single target moves existing nodes; multiple targets clone after the first.
Do not pass HTML strings from untrusted user input — XSS risk.
.prependTo() inserts inside as first child; .insertBefore() inserts as sibling above.
Since jQuery 1.9, returns the inserted element set for reliable chaining.
Before jQuery 1.9, single-target insertBefore return value made .end() chaining unreliable.
Compatibility
Browser Support
.insertBefore() has been part of jQuery since 1.0+. It relies on standard DOM insertBefore operations on the parent node. jQuery 1.9 fixed the return value for single-target insertion. Works in all browsers supported by your jQuery build — IE6+ through modern evergreen browsers in legacy jQuery versions, and all current browsers in jQuery 3.x and 4.x.
✓ jQuery 1.0+
jQuery .insertBefore()
Universal sibling insertion API with content-first syntax across jQuery 1.x, 2.x, 3.x, and 4.x. Pair with .insertAfter() for above/below placement and .before() when anchor-first syntax reads better.
100%With jQuery loaded
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()Universal
Bottom line: Default choice when you build content first and insert it before a target element.
Wrap Up
Conclusion
The jQuery .insertBefore() method inserts every matched element as the previous sibling before a target. Pass a selector, DOM element, HTML string, or jQuery object as the target — exactly as the official jQuery demos demonstrate.
Use .before() when the anchor is already selected, and .insertAfter() when content belongs below the target. For content inside a container, use .prependTo() instead. Next up: clear cached values on elements with .replaceAll().
Use .insertBefore() when building elements before choosing the anchor
Create labels with .text() for safe dynamic form text
Chain .addClass() and events before calling insertBefore
Prefer .before() when the anchor collection is already selected
Check whether content belongs inside (.prependTo) or outside (.insertBefore)
❌ Don’t
Use .insertBefore() when content should be a child inside the target
Pass untrusted HTML strings to $('<div>') constructors (XSS risk)
Confuse .insertBefore() with .prependTo() (sibling vs child)
Assume inserting always clones — single target moves existing nodes
Expect a callback on insertBefore — build dynamic nodes first
Summary
Key Takeaways
Knowledge Unlocked
Six things to remember about .insertBefore()
The content-first sibling API — insert before a target with reversed syntax.
6
Core concepts
1st01
Content
First
Syntax
→02
Target
Argument
Anchor
↔03
vs before
Reversed
Compare
1.904
Returns
Inserted
Chain
↔05
vs prependTo
Sibling
Scope
×N06
Clone
Multi target
Move
❓ Frequently Asked Questions
.insertBefore(target) inserts every element in the matched set immediately before the target element(s) as a sibling. The target can be a selector, DOM element, HTML string, array, or jQuery object. Returns a jQuery collection of the inserted elements. Available since jQuery 1.0.
Both insert content before target elements. With .before(), the anchor comes first: $('p').before('<hr>'). With .insertBefore(), the content comes first: $('<hr>').insertBefore('p'). Same DOM result, reversed syntax — like .prepend() vs .prependTo().
.insertBefore() inserts matched elements as the previous sibling before the target. .insertAfter() inserts as the next sibling after the target. Both use content-first syntax: $(content).insertBefore(target) vs $(content).insertAfter(target).
.insertBefore() inserts matched elements as siblings before the target — outside the target element. .prependTo() inserts matched elements as the first child inside the target. Use insertBefore for labels and prefixes; use prependTo to add content inside a container.
When inserting into a single target location, jQuery moves the element (not clones) and returns the inserted set. With multiple target elements, jQuery clones the inserted element for every target after the first — the original plus clones are returned.
No. Unlike .before(), which accepts a callback since 1.4, .insertBefore() only accepts a target argument. Build dynamic content first — for example $('<label>').text('Name').insertBefore('#field') — then call insertBefore on the prepared jQuery object.
Did you know?
jQuery’s four reversed-syntax pairs — .append() / .appendTo(), .prepend() / .prependTo(), .after() / .insertAfter(), and .before() / .insertBefore() — all follow the same rule: put the collection you already hold before the method, and pass the destination as the argument. jQuery 1.9 specifically fixed .insertBefore(), .insertAfter(), and .appendTo() so single-target insertion returns the inserted set, making .end() chaining predictable in widget code.