jQuery prependTo() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

.prependTo(target) inserts every matched element as the first child of the target (since jQuery 1.0). Content comes first; the target is the argument — the reverse of .prepend().

Syntax

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

jQuery
$("span").prependTo("#foo")
// same DOM result as:
$("#foo").prepend($("span"))

$("<span> Test </span>").prependTo(".inner")
$("h2").prependTo($(".container"))

// first child inside target — not a sibling
// (bottom insert = .appendTo)
CallWhat it does
prependTo()Learn jQuery .prependTo(): insert matched elements as the first child of a target, content-first twin of prepend, move vs clone, examples, pitfalls, and FAQs.

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

Parameters

ParameterDescription
Step 1Create or select the content (HTML string, existing nodes, or a jQuery object).
Step 2Choose the target: selector, element, HTML string, array, or jQuery object.
Step 3Call .prependTo(target) — content becomes the first child inside.

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: Spans Into #foo

Move every <span> so it becomes the first child of #foo.

jQuery
// <div id="foo">FOO!</div>
// <span>I have something to say... </span>
$("span").prependTo("#foo");
// same as: $("#foo").prepend($("span"));
Try It Yourself

How It Works

  1. Select content. $("span") is what will move.
  2. Pass the target. "#foo" is the container.
  3. Insert first. The span becomes the first child inside #foo.

Example 2 — Official Demo: HTML Into .inner

Create a span and prepend it into every .inner element.

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

How It Works

  1. Parse HTML. $("<span>…") builds a new node.
  2. Match targets. Both .inner divs resolve.
  3. Clone for extras. Multiple targets get copies (last keeps the original pattern).

Example 3 — Official Demo: Move <h2>

Relocate an existing heading into a container as the first child.

jQuery
// Start: h2 is a sibling above .container
$("h2").prependTo($(".container"));
// Result: h2 is first child inside .container
Try It Yourself

How It Works

  1. Select the heading. $("h2") already exists in the page.
  2. One container. A single target means move, not clone.
  3. First child. The h2 becomes the first child inside .container.

Frequently Asked Questions

.prependTo(target) inserts every matched element as the first child of the target. The target can be a selector, DOM element, HTML string, array, or jQuery object. It returns the inserted set. Available since jQuery 1.0.
Same DOM result, reversed syntax. Container first: $('#feed').prepend('<li>'). Content first: $('<li>').prependTo('#feed'). Prefer prependTo when you build the content first, then choose the container.
.prependTo() inserts as the first child (top). .appendTo() inserts as the last child (bottom). Both use content-first syntax.
One target: existing nodes move (not clone). Multiple targets: clones for every target except the last — the original moves to the final target.
No. Unlike .prepend(), which accepts a callback since 1.4, .prependTo() only takes a target. Build dynamic content first — for example $('<li>').text(msg).prependTo('#feed').
Store detached nodes with .detach(), then stash.prependTo('#container') to put them back at the top. Events and .data() survive because detach keeps jQuery state.

Next: prepend() Method — Insert as First Child

Learn how to get and set text content on matched elements.

prepend() Method — Insert as First Child 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