jQuery .prepend() Method

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
DOM methods

What You’ll Learn

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

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.

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”.

📝 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
} )

Official prepend patterns

jQuery
$( "p" ).prepend( " Hello " );

⚡ Quick Reference

GoalCode
Prepend HTML string$("#feed").prepend("<li>New</li>")
Prepend text node$("p").prepend(document.createTextNode("Hi"))
Move existing element to top$("#feed").prepend($("#banner"))
Prepend with callback$("div").prepend(function(i){ return "<span>"+i+"</span>"; })
Add at bottom instead$("#feed").append("<li>")
Reversed syntax$("<li>").prependTo("#feed")

📋 .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

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.

jQuery
$( "p" ).prepend( " Hello " );
Try It Yourself

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.

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

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)
Try It Yourself

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

jQuery
$( ".item" ).prepend( function( index ) {
  return "<span class='tag'>Top #" + ( index + 1 ) + "</span> ";
});
Try It Yourself

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" );
Try It Yourself

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.

🚀 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.

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

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.prepend() Universal

Bottom line: Default choice for adding content as the first child inside matched elements.

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

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about .prepend()

The add-at-the-top API — first child insertion made easy.

5
Core concepts
<> 02

HTML

Or DOM

Content
03

vs append

Last child

Compare
fn 04

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.

Next: jQuery .prependTo() Method

Insert matched elements at the beginning of a target container with reversed syntax.

.prependTo() tutorial →

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.

6 people found this page helpful