jQuery prepend() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

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

Syntax

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

jQuery
$("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)
CallWhat it does
prepend()Learn jQuery .prepend(): insert HTML, text, or elements as the first child, callback form, vs append and prependTo, examples, pitfalls, and FAQs.

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

Parameters

ParameterDescription
Step 1Select the parent element(s) with jQuery.
Step 2Pass HTML, a DOM node, text node, array, or jQuery object.
Step 3Or pass a function (since 1.4) that returns content per element.

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

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.

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

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: remove() Method — Delete Elements from the DOM

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

remove() Method — Delete Elements from the DOM 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.

9 people found this page helpful