The .appendTo() method inserts matched elements as the last child of a target container — the reversed syntax of .append(). This tutorial covers target types since 1.0, the detach-and-reinsert pattern, move vs clone behavior, comparisons with .append(), .prependTo(), and .detach(), the official jQuery demo, and five practical try-it examples.
01
.appendTo()
Content first
02
Target
Container
03
.append()
Inverse
04
.detach()
Reinsert
05
Move
Not clone
06
Since 1.0
Core API
Fundamentals
Introduction
When you build an element first and then decide where it belongs, reading “append this item to that list” feels natural. jQuery provides .appendTo() for exactly that workflow — the matched elements come before the method, and the target container is the argument.
Available since jQuery 1.0, .appendTo( target ) performs the same DOM insertion as .append() but with reversed syntax. $( "<li>New</li>" ).appendTo( "#list" ) reads like English: create the item, append it to the list. It is also the standard way to reinsert nodes 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 .appendTo() Method
.appendTo( target ) takes the current jQuery collection (the elements to insert) and appends each one as the last child inside the resolved target element(s). The return value is a jQuery collection of the inserted elements.
Since jQuery 1.9, when appending 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
$("#list").append("<li>") and $("<li>").appendTo("#list") produce the same DOM result. Use whichever syntax matches how you think about the operation — container-first vs content-first.
Foundation
📝 Syntax
jQuery .appendTo() has one form:
Append matched elements to target — since 1.0
jQuery
.appendTo( target )
target — selector, DOM element, HTML string, array, or jQuery object.
Matched elements become the last 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.
📋 .appendTo() vs .append() vs .prependTo() vs .detach()
Four related DOM insertion APIs — pick the right syntax and workflow.
.appendTo()
content first
Insert matched elements at end of target — content precedes method, target is argument
.append()
target first
Insert content at end inside matched elements — container precedes method
.prependTo()
first child
Insert matched elements at beginning of target — reversed syntax of prepend
.detach()
stash
Remove elements from DOM preserving state — pair with appendTo to reinsert
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: Append All Spans to #foo
Move every <span> on the page into the element with id foo.
All span elements moved into #foo as last children
Original locations lose the spans (moved, not cloned)
Official jQuery appendTo demo pattern
How It Works
The matched collection (span elements) is inserted at the end of #foo. Existing elements inside #foo remain; spans are appended after them. Spans disappear from their original positions in the document.
Example 2 — Reversed Syntax: Build Then Append
Create a list item and append it to a container — content-first reads naturally.
<li>New item</li> appended to #todo-list
Chain .text() before .appendTo() on the new element
Same DOM as $("#todo-list").append(...)
How It Works
Building the element first and chaining .appendTo() at the end keeps creation logic together. This pattern is popular when configuring 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 panel off-DOM, then restore it with .appendTo() — events and data intact.
jQuery
var panel = $( "#sidebar-panel" ).detach();
// ... later, user opens sidebar again ...
panel.appendTo( "#sidebar-container" );
Panel detached from DOM — stored in variable
appendTo("#sidebar-container") reinserts it
Click handlers and .data() still work
Classic detach + appendTo workflow
How It Works
.detach() removes the panel but preserves jQuery state. .appendTo() puts it back as the last 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> into a layout container — official jQuery pattern.
h2 removed from original location
Appended as last 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 appends the heading as its last child. The heading node is moved from its old parent — no duplicate created.
Example 5 — Append HTML to Multiple Targets
Insert the same markup into every .inner element — equivalent to append from the container side.
jQuery
$( "<b>Test</b>" ).appendTo( ".inner" );
// Same as: $( ".inner" ).append( "<b>Test</b>" );
// Multiple .inner targets → cloned copy in each (except move logic for elements)
<b>Test</b> appended to every .inner div
Each container gets the markup as last 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. 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 widget with $("<div>"), configure it, then .appendTo("#dashboard").
Detach reinsert — stash off-DOM panels with detach, restore with appendTo.
Drag-and-drop UIs — move dragged items into drop zones via appendTo.
Layout reflow — relocate widgets between sidebar and main content areas.
Sortable lists — append dragged list items to a new parent on drop.
Tab panels — append active panel into shared container after detach.
🧠 How .appendTo() Inserts Into Targets
1
Match elements to insert
jQuery collection contains nodes that will be appended.
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 appended elements — ready for further chaining.
Important
📝 Notes
Available since jQuery 1.0 — mirror of .append() with reversed syntax.
No callback form — use .append(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.
Do not pass untrusted HTML strings to jQuery constructors used with appendTo — XSS risk.
.prependTo() inserts at the beginning; .appendTo() at the end.
Compatibility
Browser Support
.appendTo() has been part of jQuery since 1.0+. It relies on standard DOM appendChild 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 .appendTo()
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.
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
.appendTo()Universal
Bottom line: Default choice for content-first insertion into a target container.
Wrap Up
Conclusion
The jQuery .appendTo() method inserts matched elements as the last child of a target container — the reversed syntax of .append(). Use it when building content first or reinserting nodes after .detach(), exactly as the official span demo demonstrates.
Choose .append() when the container comes first in your mental model, and .prependTo() for first-child insertion. Next up: reading and setting inner HTML with .html().
Use .appendTo() when building elements before choosing a container
Pair .detach() with .appendTo() for show/hide panels
Chain configuration (.text(), .addClass()) before appendTo
Use .text() on created elements for safe dynamic content
Pick append vs appendTo based on which syntax reads clearer in context
❌ Don’t
Assume appendTo always clones — single target moves existing nodes
Use untrusted HTML strings in jQuery constructors (XSS risk)
Confuse .appendTo() with .prependTo() (last vs first)
Forget that moved nodes leave their original parent
Expect a callback form on appendTo — use .append(fn) instead
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about .appendTo()
The content-first insertion API — matched elements into a target.
5
Core concepts
→01
Content
Comes first
Syntax
#02
Target
Argument
Container
↔03
.append()
Same task
Inverse
⎌04
detach
Reinsert
Pair
↦05
Move
Single target
DOM
❓ Frequently Asked Questions
.appendTo(target) inserts every matched element as the last 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 end of target elements. With .append(), the container comes first: $('#list').append('<li>'). With .appendTo(), the content comes first: $('<li>').appendTo('#list'). Same DOM result, reversed syntax — choose whichever reads more naturally.
Store detached elements: var stash = $('#panel').detach(). Later reinsert: stash.appendTo('#container'). Events and .data() survive because detach preserves jQuery state. appendTo is the standard reinsertion method after detach.
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 appendTo 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 appended as the last child inside each resolved target element.
.appendTo() inserts matched elements as the last child of the target. .prependTo() inserts as the first child. Same reversed-syntax pattern — appendTo for bottom insertion, prependTo for top insertion.
Did you know?
.appendTo() and .append() have been mirror methods since jQuery 1.0, but jQuery 1.9 changed what appendTo returns when inserting into a single element — it now returns the inserted element set instead of the original set, making .end() chaining predictable. Before that change, developers had to carefully track which collection they held after appendTo. The detach + appendTo pair remains one of the most common jQuery patterns for modal dialogs, tab panels, and sortable lists that move DOM nodes without destroying event handlers.