jQuery .appendTo() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

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

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("<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
CallWhat it does
Matched setContent to insert (existing or created)
targetSelector, Element, HTML string, Array, or jQuery
ReturnInserted set (since 1.9: original + clones when multi-target)

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Create or select the element(s) you want to insert.
Step 2Call .appendTo(target) with a selector, element, HTML string, array, or jQuery object.
Step 3Those elements become the last child of each resolved target.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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.

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: .append() Method

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

.append() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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