jQuery .prependTo() Method

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

What You’ll Learn

The .prependTo() method inserts matched elements as the first child of a target container — the reversed syntax of .prepend(). This tutorial covers target types since 1.0, the detach-and-reinsert pattern, move vs clone behavior, comparisons with .prepend(), .appendTo(), and .detach(), the official jQuery demo, and five practical try-it examples.

01

.prependTo()

Content first

02

Target

Container

03

.prepend()

Inverse

04

.detach()

Reinsert

05

Move

Not clone

06

Since 1.0

Core API

Introduction

When you build an element first and want it at the top of a list or feed, reading “prepend this alert to the notification panel” feels natural. jQuery provides .prependTo() for exactly that workflow — the matched elements come before the method, and the target container is the argument.

Available since jQuery 1.0, .prependTo( target ) performs the same DOM insertion as .prepend() but with reversed syntax. $( "<li>New</li>" ).prependTo( "#feed" ) reads like English: create the item, prepend it to the feed. It is also the standard way to reinsert nodes at the top after .detach().

The target can be a selector, DOM element, HTML string, array, or jQuery object. When moving an existing element into a single target, jQuery moves it rather than cloning. Browse the jQuery DOM hub for related manipulation methods.

Understanding the .prependTo() Method

.prependTo( target ) takes the current jQuery collection (the elements to insert) and prepends each one as the first child inside the resolved target element(s). The return value is a jQuery collection of the inserted elements.

Since jQuery 1.9, when prepending to a single target, the returned collection contains the inserted elements (making .end() chaining reliable). With multiple targets, clones are created for all but the last target.

💡
Beginner Tip

$("#feed").prepend("<li>") and $("<li>").prependTo("#feed") produce the same DOM result. Use whichever syntax matches how you think about the operation — container-first vs content-first.

📝 Syntax

jQuery .prependTo() has one form:

Prepend matched elements to target — since 1.0

jQuery
.prependTo( target )
  • target — selector, DOM element, HTML string, array, or jQuery object.
  • Matched elements become the first children inside each resolved target.

Return value

  • Returns a jQuery collection of the inserted elements.
  • Since jQuery 1.9, single-target insertion returns the moved/cloned set reliably.

Official prependTo pattern

jQuery
$( "span" ).prependTo( "#foo" );

Equivalent container-first form: $( "#foo" ).prepend( $( "span" ) ).

⚡ Quick Reference

GoalCode
Prepend element to container$("<li>").prependTo("#feed")
Move existing node to top$("#widget").prependTo("#sidebar")
Reinsert after detachstash.prependTo("#container")
Prepend to jQuery target$("h2").prependTo($(".container"))
Container-first equivalent$("#feed").prepend($("<li>"))
Insert at bottom instead$("<li>").appendTo("#feed")

📋 .prependTo() vs .prepend() vs .appendTo() vs .detach()

Four related DOM insertion APIs — pick the right syntax and workflow.

.prependTo()
content first

Insert matched elements at beginning of target — content precedes method, target is argument

.prepend()
target first

Insert content at top inside matched elements — container precedes method

.appendTo()
last child

Insert matched elements at end of target — reversed syntax of append

.detach()
stash

Remove elements from DOM preserving state — pair with prependTo to reinsert at top

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 demo and reversed-syntax patterns.

Example 1 — Official Demo: Prepend All Spans to #foo

Move every <span> on the page into the element with id foo at the top.

jQuery
$( "span" ).prependTo( "#foo" );
Try It Yourself

How It Works

The matched collection (span elements) is inserted at the beginning of #foo. Existing elements inside #foo remain but shift after the prepended spans. Spans disappear from their original positions in the document.

Example 2 — Reversed Syntax: Build Then Prepend

Create a notification item and prepend it to a feed — content-first reads naturally.

jQuery
$( "<li>" )
  .text( "New alert" )
  .prependTo( "#notifications" );

// Equivalent: $( "#notifications" ).prepend( $( "<li>" ).text( "New alert" ) );
Try It Yourself

How It Works

Building the element first and chaining .prependTo() at the end keeps creation logic together. This pattern is popular for newest-first feeds where you configure the new node (classes, data, events) before choosing its destination.

📈 Practical Patterns

Detach reinsertion, moving headings, and multi-target insertion.

Example 3 — Reinsert After .detach()

Stash a banner off-DOM, then restore it at the top with .prependTo() — events and data intact.

jQuery
var banner = $( "#promo-banner" ).detach();

// ... later, user returns to page section ...
banner.prependTo( "#content-area" );
Try It Yourself

How It Works

.detach() removes the banner but preserves jQuery state. .prependTo() puts it back as the first child of the container. This avoids rebuilding markup or rebinding events on every show/hide cycle.

Example 4 — Move a Heading Into a Container

Relocate an existing <h2> to the top of a layout container — official jQuery pattern.

jQuery
$( "h2" ).prependTo( $( ".container" ) );
Try It Yourself

How It Works

The target can be a jQuery object, not just a selector string. jQuery resolves .container and prepends the heading as its first child. The heading node is moved from its old parent — no duplicate created.

Example 5 — Prepend HTML to Multiple Targets

Insert the same markup at the top of every .inner element — official jQuery pattern from the prepend docs.

jQuery
$( "<b>Test</b>" ).prependTo( ".inner" );

// Same as: $( ".inner" ).prepend( "<b>Test</b>" );
// Multiple .inner targets → cloned copy in each (except move logic for elements)
Try It Yourself

How It Works

When the matched set is newly created HTML, each target receives its own parsed copy at the top. When moving an existing single DOM node to multiple targets, jQuery clones for all but the last. Understand this distinction to avoid surprise duplicates.

🚀 Common Use Cases

  • Build-then-place — create a notification with $("<li>"), configure it, then .prependTo("#feed").
  • Detach reinsert — stash off-DOM banners with detach, restore at top with prependTo.
  • Newest-first feeds — prepend alerts so the latest item appears first without re-sorting.
  • Layout reflow — move a heading or toolbar to the top of a shared container.
  • Sortable lists — prepend dragged list items to a new parent on drop for priority placement.
  • Tab panels — prepend active panel into shared container after detach.

🧠 How .prependTo() Inserts Into Targets

1

Match elements to insert

jQuery collection contains nodes that will be prepended.

Content
2

Resolve target container

Selector, element, or jQuery object identifies destination parent(s).

Target
3

Move or clone nodes

Single target moves existing nodes; multiple targets clone except last.

DOM
4

Return inserted set

jQuery collection of prepended elements — ready for further chaining.

📝 Notes

  • Available since jQuery 1.0 — mirror of .prepend() with reversed syntax.
  • No callback form — use .prepend(function) on the container for per-element insertion.
  • Single-target move returns the inserted element set since jQuery 1.9 (reliable .end()).
  • Multiple targets clone existing DOM nodes for all but the last destination.
  • Pair with .detach() for temporary removal and later reinsertion at the top.
  • Do not pass untrusted HTML strings to jQuery constructors used with prependTo — XSS risk.
  • .appendTo() inserts at the end; .prependTo() at the beginning.

Browser Support

.prependTo() has been part of jQuery since 1.0+. It relies on standard DOM insertBefore 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 .prependTo()

Universal DOM insertion API across jQuery 1.x, 2.x, 3.x, and 4.x. No browser-specific polyfills required — jQuery normalizes target resolution and move-or-clone logic. Pair with .detach() for stash-and-restore workflows at the top of containers.

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

Bottom line: Default choice for content-first insertion at the top of a target container.

Conclusion

The jQuery .prependTo() method inserts matched elements as the first child of a target container — the reversed syntax of .prepend(). Use it when building content first or reinserting nodes at the top after .detach(), exactly as the official span demo demonstrates.

Choose .prepend() when the container comes first in your mental model, and .appendTo() for last-child insertion. Next up: reading and writing plain text safely with .text().

💡 Best Practices

✅ Do

  • Use .prependTo() when building elements before choosing a container
  • Pair .detach() with .prependTo() for show/hide banners at top
  • Chain configuration (.text(), .addClass()) before prependTo
  • Use .text() on created elements for safe dynamic notification text
  • Pick prepend vs prependTo based on which syntax reads clearer in context

❌ Don’t

  • Assume prependTo always clones — single target moves existing nodes
  • Use untrusted HTML strings in jQuery constructors (XSS risk)
  • Confuse .prependTo() with .appendTo() (first vs last child)
  • Forget that moved nodes leave their original parent
  • Expect a callback form on prependTo — use .prepend(fn) instead

Key Takeaways

Knowledge Unlocked

Five things to remember about .prependTo()

The content-first top insertion API — matched elements into a target.

5
Core concepts
# 02

Target

Argument

Container
03

.prepend()

Same task

Inverse
04

detach

Reinsert

Pair
05

First

Child

Position

❓ Frequently Asked Questions

.prependTo(target) inserts every matched element as the first child of the target container. The target can be a selector, DOM element, HTML string, array, or jQuery object. Returns a jQuery collection of the inserted elements. Available since jQuery 1.0.
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 — choose whichever reads more naturally.
.prependTo() inserts matched elements as the first child of the target. .appendTo() inserts as the last child. Same reversed-syntax pattern — prependTo for top insertion, appendTo for bottom insertion.
Store detached elements: var stash = $('#panel').detach(). Later reinsert at the top: stash.prependTo('#container'). Events and .data() survive because detach preserves jQuery state. prependTo is ideal when restored content should appear first inside the container.
When inserting into a single target, jQuery moves the element (not clones). With multiple targets, clones are created for every target except the last — the original moves to the final target. Since jQuery 1.9, single-target prependTo returns the inserted element set.
A CSS selector string ('#foo'), a DOM element, an HTML string, an array of elements, or a jQuery object. The matched set is prepended as the first child inside each resolved target element.
Did you know?

.prependTo() and .prepend() have been mirror methods since jQuery 1.0, but jQuery 1.9 changed what prependTo returns when inserting into a single element — it now returns the inserted element set instead of the original set, making .end() chaining predictable. Notification feeds and social timelines often use prepend or prependTo because newest items belong at the top — the DOM order matches user expectations without sorting arrays. The detach + prependTo pair is ideal when a banner or toolbar should reappear at the top of a section without losing its click handlers.

Next: jQuery .text() Method

Get or set plain text contents safely — the XSS-safe alternative to .html().

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

6 people found this page helpful