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
Fundamentals
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.
Concept
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.
Foundation
📝 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.
📋 .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
Hands-On
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.
All span elements moved into #foo as first children
Original locations lose the spans (moved, not cloned)
Official jQuery prependTo demo pattern
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.
<li>New alert</li> prepended to #notifications
Chain .text() before .prependTo() on the new element
Same DOM as $("#notifications").prepend(...)
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" );
Banner detached from DOM — stored in variable
prependTo("#content-area") reinserts it at the top
Click handlers and .data() still work
Classic detach + prependTo workflow
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.
h2 removed from original location
Prepended as first child of .container
Single target → element moves (not cloned)
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)
<b>Test</b> prepended to every .inner div
Each container gets the markup as first child
Fresh HTML string → new node per target
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.
Applications
🚀 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.
Important
📝 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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.prependTo()Universal
Bottom line: Default choice for content-first insertion at the top of a target container.
Wrap Up
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().
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about .prependTo()
The content-first top insertion API — matched elements into a target.
5
Core concepts
→01
Content
Comes first
Syntax
#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.