The .before() method inserts content immediately before each matched element as a sibling. This tutorial covers HTML strings, text nodes, DOM elements, jQuery objects, multiple arguments, and the callback form since 1.4, comparisons with .after(), .insertBefore(), and .prepend(), the official jQuery demos, and five practical try-it examples.
01
.before()
Prev sibling
02
HTML
Strings
03
DOM
Move nodes
04
Callback
Since 1.4
05
vs .after()
Sibling pair
06
Since 1.0
Core API
Fundamentals
Introduction
Section headings, field labels, breadcrumb prefixes, and intro paragraphs often need to appear right above an existing element — not inside it. When content should sit as the previous sibling before a matched node, jQuery provides .before().
Available since jQuery 1.0, .before() is the mirror of .after(). Where .after() inserts content below the element, .before() inserts it above. You can pass HTML strings, text nodes, DOM elements, jQuery objects, multiple arguments, or a callback function (since 1.4).
.before() and .insertBefore() perform the same task with reversed syntax. Use .prepend() when content belongs inside the element. Browse the jQuery DOM hub for related manipulation methods.
Concept
Understanding the .before() Method
.before( content [, content ] ) inserts content immediately before each matched element in the DOM tree. The matched element itself is unchanged; new nodes appear as its preceding siblings. The return value is the same jQuery collection (for chaining).
When inserting an element already in the document before a single target, jQuery moves it rather than copying it. Since jQuery 1.4, the callback form .before( function( index ) ) runs once per matched element; return content to insert before that element.
💡
Beginner Tip
$(".inner").before(" Test ") adds “ Test ” before every .inner element — the official jQuery demo shows text appearing above “Hello” and “Goodbye”, not inside them.
Foundation
📝 Syntax
jQuery .before() supports three forms:
Insert before — since 1.0
jQuery
.before( content [, content ] )
content — HTML string, DOM element, text node, array, or jQuery object.
Multiple arguments are inserted in order before each matched element.
Insert with callback — since 1.4
jQuery
.before( function( index [, html ] ) {
// return content to insert; `this` is the current element
} )
📋 .before() vs .after() vs .insertBefore() vs .prepend()
Four related insertion APIs — pick the right position and syntax.
.before()
prev sibling
Insert content immediately before matched elements — anchor first: $(target).before(content)
.after()
next sibling
Insert content immediately after matched elements
.insertBefore()
same task
Insert before target — content first: $(content).insertBefore(target)
.prepend()
first child
Insert content inside matched elements as the first child
Hands-On
Examples Gallery
Examples 1–3 follow the official jQuery API documentation. Examples 4–5 cover the callback form and multiple-argument insertion from the docs. Use DevTools or the Try-it links to run each snippet in the browser.
📚 Getting Started
Official jQuery demos for HTML strings, text nodes, and existing elements.
Example 1 — Official Demo: Insert HTML Before Paragraphs
Insert “ Hello ” immediately before every paragraph.
Before: <p>is what I said...</p>
After: Hello <p>is what I said...</p>
" Hello " appears as sibling before each <p> — not inside it
How It Works
jQuery inserts the string as the previous sibling before each matched element. The paragraph content is unchanged; the new text sits outside and above the opening tag. Avoid untrusted HTML strings to prevent XSS.
Example 2 — Official Demo: Insert a Text Node Before
Insert plain text with document.createTextNode() instead of an HTML string.
Text node "Hello" inserted before each paragraph
No HTML parsing — safer for plain text
Official jQuery Example 2 pattern
How It Works
Text nodes bypass HTML parsing. The text appears immediately before each paragraph as a sibling node, exactly like the HTML string version but without interpreting tags.
📈 Practical Patterns
Moving elements, callback insertion, and multiple-argument sibling insertion.
Example 3 — Official Demo: Insert a jQuery Object Before
Insert an existing <b> element before paragraphs — jQuery moves it from its original location.
jQuery
$( "p" ).before( $( "b" ) );
// <b>Hello</b> moves to precede each <p>
// (cloned for all but last target if multiple matches)
<b> removed from original location
Inserted as sibling before matched <p> elements
Single target → element moves (not cloned)
How It Works
Inserting an existing DOM node before one target moves it to precede that element. With multiple targets, clones are created for all but the last match — same move-or-clone rules as .append() and .prepend().
Example 4 — Insert Before With Callback Function
Generate unique content before each paragraph using the callback form (since 1.4) — official jQuery pattern.
Each <p> gets a <b> sibling showing its className
Callback runs once per matched element
<b> appears before the paragraph, not inside it
How It Works
The callback receives the index (and optionally old HTML since 1.10). Return a string, DOM node, or jQuery object to insert before the current element. Inside the function, this refers to the DOM element being processed.
Example 5 — Multiple Arguments Before First Paragraph
Pass several DOM elements and HTML strings in one call — official jQuery multi-argument pattern.
Three nodes inserted before first <p> in order:
1. jQuery div.note
2. createElement div
3. existing #foo element (moved)
Equivalent to .before(a, b, c) with separate arguments
How It Works
jQuery accepts arrays of nodes alongside individual arguments. Each item is inserted in order before the first paragraph. Existing elements move to the new location; newly created nodes become preceding siblings. Useful for inserting a header block, label, and icon row in one call.
Applications
🚀 Common Use Cases
Section headings — insert an <h2> or label immediately before a content block.
Form field labels — add helper text or required markers above input wrappers.
Breadcrumb prefixes — prepend navigation links as siblings before page titles.
Horizontal rules — add <hr> separators before new sections.
Relocate elements — move an existing heading or banner to precede a container.
Dynamic labels — use the callback form to insert badges showing each element’s class name.
🧠 How .before() Inserts Content
1
Match anchor elements
jQuery collection points at elements that will stay in place.
Select
2
Parse or accept content
HTML strings parsed; DOM nodes and jQuery objects inserted directly.
Content
3
Insert as previous sibling
Content placed immediately before each matched element in the parent.
Before
4
⬆
Return jQuery collection
Same anchor collection since jQuery 1.9 — ready for chaining.
Important
📝 Notes
Available since jQuery 1.0 — callback form added in jQuery 1.4.
Inserts as the previous sibling; use .after() for the next sibling.
Matched elements are unchanged — content goes outside, not inside.
Inserting before a single target moves existing nodes; multiple targets clone except last.
Do not pass HTML strings from untrusted user input — XSS risk.
.prepend() inserts inside as first child; .before() inserts as sibling above.
Since jQuery 1.9, .before() always returns the original matched set.
Multiple arguments supported: .before(a, b, c).
Compatibility
Browser Support
.before() has been part of jQuery since 1.0+, with the callback form since 1.4+. It relies on standard DOM insertBefore operations on the parent node. 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 .before()
Universal sibling insertion API across jQuery 1.x, 2.x, 3.x, and 4.x. No browser-specific polyfills required — jQuery normalizes previous-sibling insertion. Pair with .after() for above/below sibling patterns.
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
.before()Universal
Bottom line: Default choice for inserting content immediately before matched elements.
Wrap Up
Conclusion
The jQuery .before() method inserts content as the previous sibling before each matched element. Pass HTML strings, text nodes, DOM elements, jQuery objects, multiple arguments, or a callback function — exactly as the official jQuery demos demonstrate.
Use .after() when content belongs below the element, and .insertBefore() when content-first syntax reads better. For content inside a container, use .prepend() or .append() instead. Next up: duplicating elements with .clone().
Use .before() for headings, labels, and prefixes above an element
Create elements with .text() for safe dynamic label text
Use the callback form for per-element prefixes or class-name badges
Prefer .insertBefore() when building content before choosing the anchor
Check whether content belongs inside (.prepend) or outside (.before)
❌ Don’t
Use .before() when content should be a child inside the element
Pass untrusted HTML strings to .before() (XSS risk)
Confuse .before() with .prepend() (sibling vs child)
Assume inserting always clones — single target moves existing nodes
Call .before() on elements without a parent (no effect since 1.9)
Summary
Key Takeaways
Knowledge Unlocked
Six things to remember about .before()
The insert-above-sibling API — previous sibling insertion made easy.
6
Core concepts
↑01
Prev
Sibling
Insert
<>02
HTML
Or DOM
Content
↓03
vs after
Next sibling
Compare
fn04
Callback
Since 1.4
Dynamic
↔05
vs prepend
Inside
Scope
1.906
Returns
Original set
Chain
❓ Frequently Asked Questions
.before() inserts the specified content immediately before each element in the matched set — as a sibling, not inside the element. It accepts HTML strings, DOM elements, text nodes, arrays, jQuery objects, multiple arguments, or a callback function (since 1.4). Returns the same jQuery collection for chaining. Available since jQuery 1.0.
.before() inserts content as the previous sibling before each matched element. .after() inserts content as the next sibling after each matched element. Both operate outside the matched element; neither adds children inside it.
Both insert content before target elements. With .before(), the matched element is the anchor: $('p').before('<hr>'). With .insertBefore(), the content comes first: $('<hr>').insertBefore('p'). Same DOM result, reversed syntax — like .append() vs .appendTo().
.before() inserts siblings outside and above the matched element. .prepend() inserts children inside the matched element as the first child. Use before for labels, headings, or prefixes above a block; use prepend to add content inside a container.
When you insert an existing DOM element before a single target, jQuery moves it (not clones). If you call .before() on multiple matched elements, jQuery clones the inserted element for every target except the last — the original moves to the final target.
Since jQuery 1.4, yes. .before(function(index)) runs once per matched element. Return an HTML string, DOM node, or jQuery object to insert before that element. Inside the function, this refers to the current DOM element. Since jQuery 1.10, a two-argument form also receives the old HTML value.
Did you know?
jQuery’s .before() and .after() have been sibling-insertion pairs since version 1.0, while .insertBefore() and .insertAfter() offer the same behavior with content-first syntax — mirroring the .append() / .appendTo() pattern. Before jQuery 1.9, calling .before() on disconnected nodes could return an unpredictable collection; modern jQuery always returns the original matched set, making method chaining reliable again.