AD JB Toolbox 01 EN

jQuery .appendTo(): Insert Content as the Last Child

.appendTo() inserts content as the target’s last child. It can create new content from HTML or move an existing element to that position.

Syntax and behavior

$(content).appendTo(target);

This is useful for creating and styling a new item before inserting it. Select or create the content before the method, then pass the destination as its argument. The Document Object Model (DOM) is the browser’s tree of document nodes. This method inserts inside the target, rather than creating a sibling outside it. The returned jQuery object contains the inserted elements, so a chained method operates on them.

The official jQuery .appendTo() documentation describes additional call signatures and argument types.

Complete runnable example

Save the following code as a UTF-8 example.html file and open it in a browser. It loads the full jQuery 4.0.0 build from an external URL, so an internet connection is required. The $(function () { ... }) callback waits for the document to be ready before running the example.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery appendTo example</title>
  <style>
    body { margin: 24px; font-family: sans-serif; line-height: 1.6; }
    .marked { background: #fff0b3; border: 2px solid #835b00; }
    .active { color: #a00000; font-weight: bold; }
    button, input, select { font: inherit; }
    #output { white-space: pre-wrap; overflow-wrap: anywhere; }
  </style>
  <script src="https://code.jquery.com/jquery-4.0.0.min.js"></script>
</head>
<body>
<ul id="list"><li>Existing item</li></ul>
  <p id="output" aria-live="polite"></p>
  <script>
    $(function () {
      $("<li>New item</li>").appendTo("#list");
      $("#output").text($("#list").children().map(function () { return $(this).text(); }).get().join(" / "));
    });
  </script>
</body>
</html>

Expected result

Opening the file inserts the new content once. The result paragraph shows Existing item / New item. In the browser’s Elements panel, check that the new li is a child of the list. Reloading the file starts again from the original markup.

Moving an existing element

$("#existing").appendTo("#destination");

Use this snippet in a document containing #existing and #destination. With one destination, the existing node changes position or parent. Inspect the live DOM rather than the original page source to see the change.

Inserting input as plain text

const userText = "<Help text>";
$("<li>").text(userText).appendTo("#list");

Replace the insertion line in the complete example with this snippet. The angle brackets appear as text. To undo the insertion, select only the newly added item and call .remove(), keeping the existing content outside that selection.

Important details and troubleshooting

  • Inserting an existing element into one destination moves the original node rather than copying it. Use .clone() before insertion if the original must remain in place.
  • With multiple destinations, jQuery creates copies for every destination except the last, which receives the original. Avoid cloning an id into several places; use classes or unique IDs.
  • If no destination matches, no inserted content appears in the document. Check the selector and when the script runs. A click handler that inserts content will add another item on each click.
  • Do not build HTML strings from untrusted input. This can introduce cross-site scripting (XSS). Create an element and use .text() to insert user-provided text without interpreting it as markup.

Compare the behavior with .append() inside insertion, .prependTo() with content first.

More in This Category
jQuery .each(): Iterate over Elements and Stop a Loop

jQuery .each(): Iterate over Elements and Stop a Loop

Use callback indices and element references, stop a loop correctly, and avoid confusing element iteration with asynchronous sequencing.

jQuery :contains(): Match Elements by Text Content

jQuery :contains(): Match Elements by Text Content

Explore case-sensitive substring matching, descendant text, and a safer way to handle dynamic search strings without constructing selectors.

jQuery .add(): Combine Element Selections

jQuery .add(): Combine Element Selections

Combine selections without changing the original collection, and distinguish selecting elements from inserting them into the document.

jQuery .remove(): Remove Elements, Data, and Events

jQuery .remove(): Remove Elements, Data, and Events

Remove selected elements and their descendants, understand selector filtering, and compare empty and detach for reuse.

jQuery .empty(): Clear Content and Keep the Container

jQuery .empty(): Clear Content and Keep the Container

Clear a container’s elements and text without deleting it, refill it, and understand how field values and event handlers differ.

jQuery .css(): Read, Set, and Clear Styles

jQuery .css(): Read, Set, and Clear Styles

Read computed styles, set and clear inline properties, and handle CSS units and numeric values in jQuery 4.0.

jQuery .val(): Read and Set Form Field Values

jQuery .val(): Read and Set Form Field Values

Read and set current field values and multiple selections, and handle change events separately from value assignment.

jQuery .addClass(): Add One or More CSS Classes

jQuery .addClass(): Add One or More CSS Classes

Add one or more classes while preserving existing names, generate classes with a callback, and check why styling may not change.

jQuery .slideDown(): Expand and Show Hidden Content

jQuery .slideDown(): Expand and Show Hidden Content

Reveal a hidden panel and handle its initial state, animation queue, callbacks, reset behavior, and reduced motion.

jQuery .slideToggle(): Toggle a Collapsible Panel

jQuery .slideToggle(): Toggle a Collapsible Panel

Build a collapsible panel with a state-aware button, animation queue control, and collection-wide completion handling.