The .prepend() method inserts content as the first child inside each matched element. This tutorial covers HTML strings, text nodes, DOM elements, jQuery objects, multiple arguments, and the callback form since 1.4, comparisons with .append(), .prependTo(), and .before(), the official jQuery demos, and five practical try-it examples.
01
.prepend()
First child
02
HTML
Strings
03
DOM
Move nodes
04
Callback
Since 1.4
05
vs .append()
Last child
06
Since 1.0
Core API
Fundamentals
Introduction
Activity feeds, notification trays, and chat headers often need new items at the top — the latest alert appears first, above older entries. When content should be inserted as the first child inside a container, jQuery provides .prepend().
Available since jQuery 1.0, .prepend() is the mirror of .append(). Where .append() adds nodes at the bottom, .prepend() adds them at the top. You can pass HTML strings, text nodes, DOM elements, jQuery objects, multiple arguments, or a callback function (since 1.4).
.prepend() and .prependTo() perform the same task with reversed syntax. Use .append() when new items belong at the bottom. Browse the jQuery DOM hub for related manipulation methods.
Concept
Understanding the .prepend() Method
.prepend( content [, content ] ) inserts content before the first existing child of each matched element. Existing children shift down. The return value is the same jQuery collection (for chaining).
When prepending an element already in the document to a single target, jQuery moves it rather than copying it. Since jQuery 1.4, the callback form .prepend( function( index, html ) ) runs once per matched element; return content to insert at the top.
💡
Beginner Tip
$(".inner").prepend(" Test ") adds “ Test ” before existing content inside every .inner element — the official jQuery demo shows text appearing ahead of “Hello” and “Goodbye”.
Foundation
📝 Syntax
jQuery .prepend() supports three forms:
Prepend content — since 1.0
jQuery
.prepend( content [, content ] )
content — HTML string, DOM element, text node, array, or jQuery object.
Multiple arguments are inserted in order as the first children.
Prepend with callback — since 1.4
jQuery
.prepend( function( index, html ) {
// return content to insert; `this` is the current element
} )
📋 .prepend() vs .append() vs .prependTo() vs .before()
Four related insertion APIs — pick the right position and syntax.
.prepend()
first child
Insert content at the beginning inside matched elements — container first
.append()
last child
Insert content at the end inside matched elements
.prependTo()
same task
Insert at beginning — content first: $(el).prependTo(target)
.before()
sibling
Insert content before the matched element itself — outside, not inside
Hands-On
Examples Gallery
Examples 1–5 follow the official jQuery API documentation and common production patterns. 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: Prepend HTML String
Prepend “ Hello ” as the first child of every paragraph.
Before: <p>there, friend!</p>
After: <p> Hello there, friend!</p>
" Hello " inserted before existing text — first child position
How It Works
jQuery inserts the string before the first child inside each matched element. Existing content remains but shifts after the prepended nodes. Avoid untrusted HTML strings to prevent XSS.
Example 2 — Official Demo: Prepend a Text Node
Insert plain text with document.createTextNode() instead of an HTML string.
Text node "Hello " prepended — no HTML parsing
Safer for user-supplied plain text
Official jQuery Example 2 pattern
How It Works
Text nodes bypass HTML parsing. The prepended text appears before existing paragraph content, exactly like the HTML string version but without interpreting tags.
📈 Practical Patterns
Moving elements, callback insertion, and newest-first feeds.
Example 3 — Official Demo: Prepend a jQuery Object
Prepend an existing <b> element into paragraphs — jQuery moves it from its original location.
jQuery
$( "p" ).prepend( $( "b" ) );
// <b>Hello</b> moves to the start of each <p>
// (cloned for all but last target if multiple matches)
<b> removed from original location
Prepended as first child of matched <p> elements
Single target → element moves (not cloned)
How It Works
Prepending an existing DOM node to one target moves it to the top of that container. With multiple targets, clones are created for all but the last match — same move-or-clone rules as .append().
Example 4 — Prepend With Callback Function
Generate unique prepended content per element using the callback form (since 1.4).
Each .item gets "Top #1", "Top #2" prepended
Callback runs once per matched element
Content appears before existing children
How It Works
The callback receives index and old inner HTML. Return a string, DOM node, or jQuery object to prepend. Useful for per-item prefixes or badges at the top of containers.
Example 5 — Newest-First Notification Feed
Prepend new alerts to the top of a feed so the latest item appears first.
jQuery
function addNotification( message ) {
var $item = $( "<li class='alert'>" ).text( message );
$( "#notifications" ).prepend( $item );
}
addNotification( "New message received" );
New <li> appears at top of #notifications
Older items shift down — no re-sort needed
Use .text() for safe message content
Classic prepend use case vs append for chat logs
How It Works
Activity feeds that show newest-first use .prepend() instead of .append(). Building the element with .text(message) avoids XSS from notification text. Pair with a max-item limit and .remove() on overflow.
Applications
🚀 Common Use Cases
Notification feeds — prepend new alerts so the latest appears at the top.
Activity streams — Twitter-style timelines with newest entries first.
Table headers in tbody — prepend a summary row above data rows.
Breadcrumb prefixes — prepend a home link inside a nav container.
Form hints — prepend helper text above existing field groups.
Move banner to top — prepend an existing promo element inside a layout shell.
🧠 How .prepend() Inserts Content
1
Match container elements
jQuery collection points at parent nodes receiving content.
Select
2
Parse or accept content
HTML strings parsed; DOM nodes and jQuery objects inserted directly.
Content
3
Insert as first child
Content added before all existing children inside each match.
Prepend
4
⬆
Return jQuery collection
Same container collection — ready for chaining or event binding.
Important
📝 Notes
Available since jQuery 1.0 — callback form added in jQuery 1.4.
Inserts as the first child; use .append() for the last child.
Existing children remain and shift after prepended content.
Appending to single target moves existing nodes; multiple targets clone except last.
Do not prepend HTML strings from untrusted user input — XSS risk.
.before() inserts as a sibling before the element; .prepend() inserts inside.
Multiple arguments supported: .prepend(a, b, c).
Compatibility
Browser Support
.prepend() has been part of jQuery since 1.0+, with the callback form since 1.4+. It relies on standard DOM insertBefore/appendChild operations. 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 .prepend()
Universal DOM insertion API across jQuery 1.x, 2.x, 3.x, and 4.x. No browser-specific polyfills required — jQuery normalizes first-child insertion. Pair with .append() for bottom vs top insertion 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
.prepend()Universal
Bottom line: Default choice for adding content as the first child inside matched elements.
Wrap Up
Conclusion
The jQuery .prepend() method inserts content as the first child inside 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 .append() when new items belong at the bottom, and .prependTo() when content-first syntax reads better. Next up: the reversed-syntax twin of .prepend() with .prependTo().
Use .prepend() for newest-first feeds and top-of-list insertion
Create elements with .text() for safe dynamic notification text
Chain configuration before prepending newly built elements
Use the callback form for per-container prepended prefixes
Limit feed length by removing old items after prepend
❌ Don’t
Use .prepend() when items should appear at the bottom (use append)
Pass untrusted HTML strings to .prepend() (XSS risk)
Confuse .prepend() with .before() (inside vs sibling)
Assume prepending always clones — single target moves existing nodes
Prepend without considering scroll position in auto-scrolling feeds
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about .prepend()
The add-at-the-top API — first child insertion made easy.
5
Core concepts
↑01
First
Child
Insert
<>02
HTML
Or DOM
Content
↓03
vs append
Last child
Compare
fn04
Callback
Since 1.4
Dynamic
🔔05
Feeds
Newest first
Use case
❓ Frequently Asked Questions
.prepend() inserts the specified content as the first child of each element in the matched set. It accepts HTML strings, DOM elements, text nodes, arrays, jQuery objects, or a callback function (since 1.4). Returns the same jQuery collection for chaining. Available since jQuery 1.0.
.prepend() inserts content as the first child inside each matched element. .append() inserts as the last child. Use prepend for newest-first feeds and notifications at the top; use append for chat logs and lists that grow downward.
Both insert at the beginning of target elements. With .prepend(), the container comes first: $('#feed').prepend('<li>'). With .prependTo(), the content comes first: $('<li>').prependTo('#feed'). Same DOM result, reversed syntax.
When you prepend an existing DOM element to a single target, jQuery moves it (not clones). If you prepend to multiple targets, jQuery clones the element for every target except the last — the original moves to the final target.
Yes. Pass multiple DOM elements, HTML strings, or jQuery objects: $('#feed').prepend($div1, newdiv2, existingEl). jQuery inserts each argument in order as the first children (later args appear before earlier ones in DOM order).
Since jQuery 1.4, yes. .prepend(function(index, html)) runs once per matched element. The callback receives the index and old inner HTML; return content to insert at the top. Inside the function, this refers to the current DOM element.
Did you know?
jQuery’s .prepend() and .append() have been mirror methods since version 1.0 — together they cover the two insertion points inside a container. Social media timelines and notification drawers almost always use prepend (newest on top), while chat applications typically use append (newest at the bottom). Under the hood, prepend uses insertBefore on the container’s first child, which is why prepended nodes always appear ahead of existing content without re-sorting the list.