jQuery .appendTo() Method

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

What Is .appendTo()?

.appendTo(target) inserts every matched element as the last child of the target (since jQuery 1.0). Same job as .append() — content comes first.

Remember
$("<li>New</li>").appendTo("#list")   // content first
$("#list").append("<li>New</li>")       // container first (same result)

$("span").appendTo("#foo")              // move existing nodes
$("<div>Test</div>").appendTo(".inner") // create + insert
NeedUse
Content-first insert (end)$(content).appendTo(target)
Container-first insert (end)$(target).append(content)
Content-first insert (start)$(content).prependTo(target)
Reinsert after detachstash.appendTo("#box")

How to Use .appendTo()

Start with the content, then name the target container.

  1. Create or select the element(s) you want to insert.
  2. Call .appendTo(target) with a selector, element, HTML string, array, or jQuery object.
  3. Those elements become the last child of each resolved target.
  4. Chain on the returned inserted set when you need the moved/cloned nodes.
PieceRequired?Role
Matched setYesContent to insert (existing or created)
targetYesSelector, Element, HTML string, Array, or jQuery
Return—Inserted set (since 1.9: original + clones when multi-target)

Pseudocode

Pseudocode
content = matched elements (or newly created markup)
targets = resolve(target)   // selector / element / jQuery…

for each target t:
    if last target:
        move content into t as last child
    else:
        clone content into t as last child

return inserted set   // original + clones (jQuery 1.9+)

// Twin API:
targets.append(content)  ≡  content.appendTo(targets)

Cheat Sheet

Keep these facts handy while inserting into a target.

TopicDetail
Syntax.appendTo(target)
PositionLast child inside each target
SincejQuery 1.0
vs appendSame task; content-first vs container-first
vs prependToEnd vs start inside the target
Move vs cloneOne target moves; many targets clone
Return (1.9+)Inserted set — friendlier with .end()
With detachstash.appendTo("#box") reinserts safely

Live Preview

Move or create content into #box with .appendTo().

Browser demo of .appendTo() — same idea as the examples below.

Outside: FOO!

I have nothing more to say...

Result
$("span").appendTo("#box")
→ span moves into #box as last child

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Create Markup and Insert

Build an element on the fly, then drop it into a list.

jQuery
$("<li>Cherry</li>").appendTo("#fruits");
Try It Yourself

How It Works

  1. Create. $("<li>Cherry</li>") builds a new list item.
  2. Target. #fruits is the container that receives it.
  3. Insert. .appendTo() places the item as the last child.
  4. Order. Existing items stay; Cherry appears at the bottom.

Example 2 — Move Existing Spans (Official Pattern)

Select live nodes and append them into another element.

jQuery
$("span").appendTo("#foo");
// each <span> moves into #foo as a last child
Try It Yourself

How It Works

  1. Select content. $("span") collects existing span elements.
  2. One target. #foo receives them as last children.
  3. Move. Spans leave their old parents — they are not copied.
  4. Return. Since jQuery 1.9, you get the inserted set back for chaining.

Example 3 — .appendTo() vs .append()

Same DOM result — pick the order that reads clearer.

jQuery
$("<li>A</li>").appendTo("#list");
$("#list").append("<li>B</li>");
// both add a last-child <li> inside #list
Try It Yourself

How It Works

  1. appendTo. Content first — natural when you just created the node.
  2. append. Container first — natural when the list is already in focus.
  3. Same end. Both insert as the last child inside #list.
  4. Rule. Choose the syntax that matches how you think about the data.

Example 4 — Reinsert After .detach()

Park an element off-DOM, then put it back with .appendTo().

jQuery
var stash = $("#panel").detach();
// … later …
stash.appendTo("#container");
Try It Yourself

How It Works

  1. Detach. Removes #panel but keeps jQuery data and handlers.
  2. Hold. stash stores that collection for later.
  3. Reinsert. stash.appendTo("#container") places it as the last child.
  4. Why. Cleaner than .remove() when you need the same node again.

Edge Cases & Pitfalls

Check these before using .appendTo() in production.

xss

Untrusted HTML can execute code

Building $("<div>" + user + "</div>") is unsafe. Prefer .text() on a created element, then .appendTo().

move

Existing nodes move, not copy

One target removes the node from its old parent. Clone first if you need both places.

multi

Multiple targets clone

One source into many containers → clones for all but the last. The returned set includes those clones (jQuery 1.9+).

svg

SVG is not officially supported

jQuery does not officially support SVG documents for methods like .appendTo() — results can be unexpected.

Browser Support

.appendTo() has been part of jQuery since 1.0. Return-value behavior for single-target inserts was clarified in jQuery 1.9. It works wherever your loaded jQuery version runs.

✓ Stable · jQuery 1.0+

jQuery .appendTo()

Supported in all browsers that run jQuery 1.0+. Prefer a current jQuery 3.x build for modern projects (1.9+ return set recommended).

100% jQuery core method
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: Safe for any project that already loads jQuery. Use content-first syntax when creating nodes; use .append() when the container is already selected.

Performance Notes

ApproachCostBest for
One .appendTo() with a fragmentFewer reflowsMany nodes at once
Loop of tiny .appendTo() callsMore DOM workOccasional single inserts
.detach() + later .appendTo()Cheap park/restoreTemporary hide without losing state

Build a batch of nodes, then call .appendTo() once. Avoid inserting inside a tight loop when hundreds of nodes are involved.

Key Takeaways

  • Role: insert matched elements as the last child of a target.
  • Twin: .append() does the same job with reversed syntax.
  • Move: one target moves a live node; many targets clone.
  • Detach: stash.appendTo(target) is the standard reinsert.

One line: .appendTo() is content-first insertion at the end of a target — perfect after $("<…>") or .detach().

Frequently Asked Questions

.appendTo(target) inserts every matched element as the last child of the target. 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.
Same job, reversed syntax. With .append(), the container comes first: $('#list').append('<li>'). With .appendTo(), the content comes first: $('<li>').appendTo('#list').
With one target, jQuery moves the element. With multiple targets, it clones for every target except the last — the original moves to the final target. Since jQuery 1.9, appendTo returns the inserted set (original plus clones when needed).
Store detached elements with var stash = $('#panel').detach(). Reinsert later with stash.appendTo('#container'). Events and .data() survive because detach keeps jQuery state.
A CSS selector ('#foo'), a DOM element, an HTML string, an array of elements, or a jQuery object. Matched elements become the last child of each resolved target.
.appendTo() inserts as the last child of the target. .prependTo() inserts as the first child. Same content-first pattern — choose end vs start.
Do not build HTML strings from untrusted input — script tags and event attributes can run (XSS). Prefer $('<li>').text(userText).appendTo(target) for plain text.

Next: .html()

Get or set an element’s inner HTML in one call.

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

9 people found this page helpful