jQuery prependTo() Method

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

What Is prependTo()?

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

Remember
$("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)
NeedUse
Content first, first child$(el).prependTo(target)
Container first$(target).prepend(content)
Last child (content first).appendTo()
Sibling above (outside).insertBefore()

How to Use prependTo()

Build or select content, pass a target, insert as the first child.

  1. Create or select the content (HTML string, existing nodes, or a jQuery object).
  2. Choose the target: selector, element, HTML string, array, or jQuery object.
  3. Call .prependTo(target) — content becomes the first child inside.
  4. Chain further on the returned inserted set (reliable since jQuery 1.9).
PieceRequired?Role
jQuery collectionYesContent to insert
targetYesContainer that receives content first
Return—Inserted element set (jQuery 1.9+)

Pseudocode

Pseudocode
content = matched elements (or new HTML nodes)
targets = resolve(target)

for each t in targets:
    if last target:
        move content as first child of t
    else:
        clone content as first child of t

return inserted set

// Same idea as .prepend(), reversed syntax:
// content.prependTo(target)  ≡  target.prepend(content)

Cheat Sheet

Keep these facts handy while inserting into a target.

TopicDetail
Syntax.prependTo(target)
SincejQuery 1.0
PositionFirst child inside the target
vs .prepend()Same result; content-first vs container-first
Callback?No — build dynamic nodes first
One targetExisting nodes move
Many targetsClones except for the last

Live Preview

Run official-style prependTo actions on a tiny demo tree and see content land as the first child.

Browser demo of .prependTo() — 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: 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.
  4. Single target. The node moves — it is not cloned.

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).
  4. Inside each. The span is the first child of every .inner.

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.
  4. Return value. Since 1.9 you get the inserted set for chaining.

Example 4 — prependTo vs prepend

Same first-child result; pick the syntax that matches how you think.

jQuery
// Content first — build, then place
$("<li>").text("New").prependTo("#list");

// Container first — same DOM result
$("#list").prepend($("<li>").text("New"));
Try It Yourself

How It Works

  1. Build safely. .text() sets item text without HTML injection.
  2. Content-first. prependTo reads naturally when the node is ready first.
  3. Container-first. prepend reads naturally when the list is already selected.
  4. Same child. Both place New as the first child of #list.

Edge Cases & Pitfalls

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

inside

Child, not sibling

prependTo goes inside the target. For a sibling above, use .insertBefore().

move

One target moves nodes

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

callback

No callback form

Unlike .prepend(), build dynamic content first, then call prependTo.

xss

Untrusted HTML is unsafe

Prefer .text() for user strings. Never inject raw markup from URLs or forms.

Browser Support

.prependTo() is a core jQuery method (since 1.0). It needs a browser DOM with jQuery loaded. Return-value chaining for single-target inserts is solid since 1.9.

✓ jQuery 1.0+ · Browsers only

jQuery .prependTo()

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

Bottom line: Use .prependTo(target) in the browser for content-first first-child insertion. Prefer jQuery 1.9+ when chaining on the inserted set.

Performance Notes

ApproachCostBest for
.prependTo(one)LowSingle first-child insert / move
.prependTo(many)HigherClones per extra target — expect more work
Native Element.prependLowestHot paths without jQuery

Prefer one clear insert over many tiny DOM moves in a loop. Batch when you can, and avoid inserting untrusted HTML strings.

Key Takeaways

  • Role: insert matched content as the first child of a target.
  • Syntax: content first — mirror of .prepend().
  • vs appendTo: top vs bottom inside the target.
  • Move vs clone: one target moves; many targets clone.

One line: $(content).prependTo(target) puts nodes at the top inside the target — content-first twin of .prepend().

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: text()

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

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