jQuery .appendTo() Method

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

What You’ll Learn

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

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.

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.

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

Official appendTo pattern

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

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

⚡ Quick Reference

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

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

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.

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

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.

jQuery
$( "<li>" )
  .text( "New item" )
  .appendTo( "#todo-list" );

// Equivalent: $( "#todo-list" ).append( $( "<li>" ).text( "New item" ) );
Try It Yourself

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" );
Try It Yourself

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.

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

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)
Try It Yourself

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.

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

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

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

Bottom line: Default choice for content-first insertion into a target container.

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

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about .appendTo()

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

5
Core concepts
# 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.

Next: jQuery .html() Method

Get or set the HTML contents of matched elements with getter and setter forms.

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