jQuery prepend() Method

Beginner
6 min read
Updated: Oct 2026
4 examples
Live preview

What Is prepend()?

.prepend(content) inserts content as the first child inside each matched element (since jQuery 1.0). To insert as the last child, use .append().

Remember
$("p").prepend("<b>Hello </b>")
$(".inner").prepend("<span> Test </span>")
$(".container").prepend($("h2"))   // move existing node

// same result, reversed syntax:
$("<span> Test </span>").prependTo(".inner")

// first child inside — not a sibling
// (sibling above = .before)
NeedUse
First child inside$(target).prepend(content)
Last child inside.append()
Content first.prependTo()
Sibling above (outside).before()

How to Use prepend()

Select the container, pass content (or a callback), insert at the top.

  1. Select the parent element(s) with jQuery.
  2. Pass HTML, a DOM node, text node, array, or jQuery object.
  3. Or pass a function (since 1.4) that returns content per element.
  4. Chain further on the same collection — prepend returns the originals.
PieceRequired?Role
jQuery collectionYesContainer(s) to insert into
content / functionYesWhat goes in as the first child
Return—Same matched set (chainable)

Pseudocode

Pseudocode
for each container in matched set:
    place content as first child inside container

if content is an existing node and many containers:
    clone for all but the last container
    move original into the last container

// Official idea:
$(".inner").prepend("<span> Test </span>")
$(".container").prepend($("h2"))   // move, not clone (one target)

// Reversed twin:
content.prependTo(target)  ≡  target.prepend(content)

Cheat Sheet

Keep these facts handy while inserting at the top.

TopicDetail
Syntax.prepend(content) / .prepend(fn)
Since1.0 (callback since 1.4)
PositionFirst child inside the target
vs .append()Top vs bottom inside
vs .prependTo()Container-first vs content-first
One targetExisting nodes move
Many targetsClones except for the last

Live Preview

Run official-style prepend actions on a tiny demo tree and see the first-child order change.

Browser demo of .prepend() — same idea as the examples below.

Result
Ready — pick an action and run.

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Official Demo: Prepend HTML

Insert a bold greeting as the first child of every paragraph.

jQuery
// <p>there, friend!</p><p>amigo!</p>
$("p").prepend("<b>Hello </b>");
Try It Yourself

How It Works

  1. Select paragraphs. Both <p> elements match.
  2. Pass HTML. jQuery parses <b>Hello </b>.
  3. Insert first. Each paragraph gets that markup as its first child.
  4. Clone for extras. Multiple targets get their own copy of the new nodes.

Example 2 — Official Demo: Move an Existing Node

Prepend an existing heading into a container — jQuery moves it.

jQuery
// <h2>Greetings</h2>
// <div class="container">
//   <div class="inner">Hello</div>
//   <div class="inner">Goodbye</div>
// </div>
$(".container").prepend($("h2"));
Try It Yourself

How It Works

  1. Select the target. One .container will receive the heading.
  2. Pass existing nodes. $("h2") is already in the page.
  3. Single target → move. The h2 leaves its old spot.
  4. First child. It becomes the first child inside the container.

Example 3 — Callback Form (Since 1.4)

Return different first-child content for each matched element.

jQuery
// <div class="box">A</div><div class="box">B</div>
$(".box").prepend(function (index, oldHtml) {
  return "<strong>#" + (index + 1) + " </strong>";
});
Try It Yourself

How It Works

  1. Per element. The function runs once for each .box.
  2. index. Starts at 0 — we show #1, #2.
  3. oldHtml. Current inner HTML before the prepend (available if needed).
  4. Return value. Inserted as the first child of that element.

Example 4 — prepend vs append

Same container; first child vs last child.

jQuery
// <ul id="list"><li>middle</li></ul>
$("#list").prepend("<li>top</li>");
$("#list").append("<li>bottom</li>");
Try It Yourself

How It Works

  1. prepend. top becomes the first child.
  2. append. bottom becomes the last child.
  3. middle stays. Existing items keep their relative order.
  4. Rule. Newest-first feeds → prepend; growing logs → append.

Edge Cases & Pitfalls

Check these before using .prepend() in real pages.

inside

Child, not sibling

prepend goes inside. For a sibling above the element, use .before().

move

One target moves nodes

Existing elements leave their old spot. Multiple targets clone except for the last.

xss

Untrusted HTML is unsafe

Prefer .text() for user strings, or sanitize before prepending markup.

syntax

Container first

$("#feed").prepend(html). Content-first style is .prependTo().

Browser Support

.prepend() is a core jQuery method (since 1.0; callback since 1.4). It needs a browser DOM with jQuery loaded.

✓ jQuery 1.0+ · Browsers only

jQuery .prepend()

Works in Chrome, Firefox, Safari, Edge, and other browsers with jQuery. Not for Node.js without a DOM.

100% Browser support
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() Browsers

Bottom line: Use .prepend(content) in the browser to insert as the first child. Prefer .text() for untrusted strings.

Performance Notes

ApproachCostBest for
.prepend(html)LowSingle inserts / small feeds
.prepend many times in a loopHigherAvoid — batch or build a fragment first
Native Element.prependLowestHot paths without jQuery

Prefer one clear prepend over many tiny DOM writes in a tight loop. Build markup once when inserting several siblings at the top.

Key Takeaways

  • Role: insert content as the first child inside matched elements.
  • vs append: top vs bottom inside the same parent.
  • Twin: .prependTo() is content-first syntax.
  • Move vs clone: one target moves; many targets clone.

One line: $(target).prepend(content) puts new nodes at the top inside the target — the opposite of .append().

Frequently Asked Questions

.prepend() inserts content as the first child of each matched element. It accepts HTML strings, DOM elements, text nodes, arrays, jQuery objects, multiple arguments, or a callback (since 1.4). Returns the same jQuery collection for chaining. Available since jQuery 1.0.
.prepend() inserts as the first child (top). .append() inserts as the last child (bottom). Use prepend for newest-first feeds; use append for lists that grow downward.
Same DOM result, reversed syntax. Container first: $('#feed').prepend('<li>'). Content first: $('<li>').prependTo('#feed'). Prefer prepend when the container is already selected.
One target: existing nodes move (not clone). Multiple targets: clones for every target except the last — the original moves to the final target.
Yes, since jQuery 1.4. .prepend(function(index, html)) runs once per matched element. Return an HTML string, DOM node, or jQuery object. Inside the function, this is the current element.
.prepend() inserts inside the element as the first child. .before() inserts as a previous sibling outside the element.

Next: prependTo()

Learn the content-first syntax that inserts into a target as the first child.

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.

9 people found this page helpful