jQuery .before() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

.before(content) inserts content as the previous sibling of each matched element (since jQuery 1.0). It sits outside the element — use .prepend() to insert inside.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("p").before("<b>Hello</b>")     // anchor first
$("<b>Hello</b>").insertBefore("p") // content first (same result)

$("p").before(" Hello ")              // HTML / text
$("p").before(document.createTextNode("Hi"))
$(".box").before($("h2"))             // move existing node
CallWhat it does
Matched setAnchor element(s) — content goes before them
contentHTML, Element, Text, Array, or jQuery (*or callback)
Callback (1.4+)function(index); since 1.10 also (index, html)
Return (1.9+)Original matched set (unchanged)

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
14+Used in Callback (1.4+). function(index); since 1.10 also (index, html)
19+Used in Return (1.9+). Original matched set (unchanged)

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 — Insert HTML Before Paragraphs

Add content as the previous sibling of every <p>.

jQuery
$("p").before("<b>Hello</b> ");
Try It Yourself

How It Works

  1. Select. $("p") matches every paragraph.
  2. Parse. jQuery turns the HTML string into a <b> node.
  3. Insert. That node becomes the previous sibling of each <p>.

Example 2 — Insert a Text Node

Insert plain text without HTML parsing.

jQuery
$("p").before(document.createTextNode("Hello "));
Try It Yourself

How It Works

  1. Create. createTextNode("Hello ") builds a real text node.
  2. Insert. jQuery places it as the previous sibling of each <p>.
  3. No parse. Angle brackets stay literal text.

Example 3 — Move an Existing Element

Insert a jQuery object before the paragraph — with one target, it moves.

jQuery
$("p").before($("b"));
// <b> leaves its old parent and lands before <p>
Try It Yourself

How It Works

  1. Select content. $("b") points at an existing bold element.
  2. One target. jQuery detaches it and inserts it before the paragraph.
  3. Many targets. Clones fill every match except the last.

Frequently Asked Questions

.before() inserts content immediately before each matched element as a previous sibling — outside the element, not inside it. It accepts HTML strings, DOM elements, text nodes, arrays, jQuery objects, or a callback (since 1.4). Returns the original matched set for chaining. Available since jQuery 1.0.
.before() inserts as the previous sibling. .after() inserts as the next sibling. Both operate outside the matched element.
Same job, reversed syntax. With .before(), the anchor comes first: $('p').before('<b>Hi</b>'). With .insertBefore(), the content comes first: $('<b>Hi</b>').insertBefore('p').
.before() adds a sibling above the element. .prepend() adds a child inside the element as the first child.
With one matched target, jQuery moves the inserted element. With multiple targets, it clones for every match except the last — the original moves to the final target.
Since jQuery 1.4, yes. .before(function(index)) runs once per match. Since 1.10, the callback can also receive the old HTML: function(index, html). Return content to insert; this is the current element.
As of jQuery 1.9, .before() on a node without a parent has no effect and still returns the original set.

Next: .clone() Method

Copy elements (and optionally events/data) before inserting them elsewhere.

.clone() Method 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.

8 people found this page helpful