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

jQuery .appendTo() takes the selected content and puts it at the end inside each destination. The returned jQuery collection contains the inserted elements, so you can style or remove just the new items even when you add them to several lists.

Select the content before the destination

$(content).appendTo(target);

Select the content or create an element before calling the method. Pass a selector, a DOM element, or a jQuery object as target. The Document Object Model (DOM) represents a document as a tree of nodes. For example, $item.appendTo('#list') places the elements in $item at the end inside #list.

Official jQuery .appendTo() documentation

Add to two lists and undo the insertion

Save the code as append-to-example.html in UTF-8 and open it in a browser. It loads jQuery 4.0.0 from a CDN, so it needs an internet connection. Both lists start with one existing entry. Click Add to both lists, then Undo addition.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Add an item to two lists</title>
  <style>
    body { margin: 1rem; font: 1rem/1.5 system-ui, sans-serif; overflow-wrap: anywhere; }
    main { max-width: 42rem; margin-inline: auto; }
    .lists { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(min(100%,16rem),1fr)); }
    section { padding: .7rem 1rem; border: 1px solid #b0bec5; border-radius: .4rem; }
    h2 { font-size: 1.1rem; margin-top: 0; }
    button { font: inherit; padding: .4rem .7rem; margin: .2rem; }
    .marked { background: #ffe8a6; color: #432b00; font-weight: 600; }
  </style>
  <script src="https://code.jquery.com/jquery-4.0.0.min.js"></script>
</head>
<body>
  <main>
    <h1>Add an item to two lists</h1>
    <button type="button" id="add">Add to both lists</button>
    <button type="button" id="undo">Undo addition</button>
    <p role="status" id="status"></p>
    <div class="lists">
      <section><h2>Notices</h2><ul class="queue"><li>Existing notice</li></ul></section>
      <section><h2>Schedule</h2><ul class="queue"><li>Existing schedule entry</li></ul></section>
    </div>
  </main>
  <script>
    $(function () {
      let $added = $();
      function updateCount() {
        $("#status").text("Items added: " + $added.length);
      }
      $("#add").on("click", function () {
        if ($added.length) return;
        $added = $("<li>").text("Weekend opening").appendTo(".queue").addClass("marked");
        updateCount();
      });
      $("#undo").on("click", function () {
        $added.remove();
        $added = $();
        updateCount();
      });
      updateCount();
    });
  </script>
</body>
</html>

Adding puts Weekend opening at the end of both lists and highlights both items. The status shows 2. Repeating the action does not add duplicates. Undo removes only the two elements stored in $added, keeps the original entries, and resets the count to 0.

The example creates an empty <li> and fills it with .text(). Use the same approach for strings from forms or APIs so angle brackets are displayed as characters.

How jQuery .text() handles strings

Move existing nodes and copy to multiple targets

Putting an existing element in a single destination moves the original. Run this snippet in the example page’s console. It prints 0 true followed by true. The temporary source list is empty, and the last child of the destination is the original node.

const $source = $("<ul>").append($("<li>").text("Item to move"));
const $item = $source.children();
const $destination = $("<ul>").append($("<li>").text("Existing notice"));
const $moved = $item.appendTo($destination);
console.log($source.children().length, $moved[0] === $item[0]);
console.log($destination.children().last()[0] === $item[0]);

With multiple destinations, the last gets the original and the earlier destinations get copies. In the example, both nodes are in the returned collection. To keep an item in its original location, insert a copy made with .clone(). Give copies unique IDs or remove the copied id before insertion.

Chain methods on the inserted elements

Both .append() and .appendTo() insert at the end inside a target. Run these two chains in the example page’s console to see which elements receive .addClass().

const $left = $("<ul>");
const $right = $("<ul>");
$left.append($("<li>")).addClass("marked");
$("<li>").appendTo($right).addClass("marked");
console.log($left.hasClass("marked"), $left.children().hasClass("marked"));
console.log($right.hasClass("marked"), $right.children().hasClass("marked"));

The first line prints true false because the class lands on the list after .append(). The second prints false true because the class lands on the inserted item after .appendTo().

Method Insertion position Returned elements
.appendTo() End inside the destination Elements inserted
.append() End inside the destination Destination elements
.prependTo() Beginning inside the destination Elements inserted

In jQuery 4.0.0, calling .end() after .appendTo() goes back to the previous selection. To remove the inserted nodes from the DOM, call .remove() on the returned collection as in the example.

jQuery .end() and the previous selection

Troubleshoot unexpected insertion results

  • If the count is 0, check the target selector and when the code runs. An empty selection provides no destination.
  • If you meant to change the new items, check that you are using the return value of .appendTo().
  • In the Elements panel, inspect the last child and its classes. View Source shows the original markup.

Start with the destination in .append() · Insert at the beginning with .prependTo()