jQuery .append() Method

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

What Is .append()?

.append(content) inserts content as the last child of each matched element (since jQuery 1.0). Use .prepend() for the first child.

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

$("p").append(" Hello ")              // HTML / text
$("p").append(document.createTextNode("Hi"))
$(".box").append($("#widget"))        // move existing node
NeedUse
Add at the end (inside)$("#el").append(content)
Add at the start (inside)$("#el").prepend(content)
Content-first syntax$(content).appendTo("#el")
Replace all children$("#el").html(html)

How to Use .append()

Select the container, then pass what to insert at the end.

  1. Select the target element(s) that should receive new content.
  2. Call .append(content) with HTML, a node, a jQuery object, or a callback.
  3. Content becomes the last child of each match — existing children stay.
  4. Chain further methods on the same collection if needed.
PieceRequired?Role
Matched setYesContainer(s) that receive content
contentYes*HTML, Element, Text, Array, or jQuery (*or callback)
Callback (1.4+)Optionalfunction(index, html) → content to insert
Return—Same jQuery collection (chainable)

Pseudocode

Pseudocode
for each matched element el:
    if content is a function:
        piece = content(index, el.innerHTML)   // this = el
    else:
        piece = content

    insert piece as last child of el

    // existing DOM node + one target  → move (not clone)
    // existing DOM node + many targets → clone for all but last

return original matched set

Cheat Sheet

Keep these facts handy while inserting content.

TopicDetail
Syntax.append(content [, content…])
Callback.append(function(index, html) { … }) since 1.4
PositionLast child inside each match
SincejQuery 1.0 (callback 1.4)
vs appendToSame task; container-first vs content-first
vs prependEnd vs start inside the element
Move vs cloneOne target moves; many targets clone
SecurityNever append untrusted HTML strings

Live Preview

Append into <p id="say">I would like to say:</p> and watch the last-child insert.

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

I would like to say:

Result
$("#say").append(" Hello ")
→ I would like to say: Hello 

jQuery Examples

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

Example 1 — Append an HTML String

Add text as the last child of every paragraph.

jQuery
$("p").append(" Hello ");
Try It Yourself

How It Works

  1. Select. $("p") matches every paragraph.
  2. Parse. jQuery turns " Hello " into content (a text node here).
  3. Insert. That content becomes the last child of each <p>.
  4. Preserve. Existing text stays; only new content is added at the end.

Example 2 — Append a Text Node

Insert plain text without HTML parsing.

jQuery
$("p").append(document.createTextNode("Hello"));
Try It Yourself

How It Works

  1. Create. createTextNode("Hello") builds a real text node.
  2. Append. jQuery inserts that node as the last child.
  3. No parse. Angle brackets in the string stay literal text.
  4. When to use. Safer path for plain user text (still validate input).

Example 3 — Move an Existing Element

Append a jQuery object — with one target, the node moves.

jQuery
$("p").append($("strong"));
// <strong> leaves its old parent and lands inside <p>
Try It Yourself

How It Works

  1. Select content. $("strong") points at an existing node.
  2. One target. jQuery detaches it and inserts it as the last child.
  3. Many targets. Clones fill every match except the last.
  4. Takeaway. Append moves a live node when there is only one target.

Example 4 — Append With a Callback

Return different content per element (since jQuery 1.4).

jQuery
$(".item").append(function (index) {
  return " #" + (index + 1);
});
Try It Yourself

How It Works

  1. Per element. The callback runs once for each .item.
  2. Index. Zero-based position in the matched set.
  3. Return. Returned string/node becomes the last child.
  4. this. Inside the function, this is the current DOM element.

Edge Cases & Pitfalls

Check these before using .append() in production.

xss

Untrusted HTML can execute code

Script tags and event attributes in appended strings are dangerous. Prefer .text() for plain text.

move

Existing nodes move, not copy

Appending a live element to one target removes it from its old parent. Clone first if you need both places.

multi

Multiple targets clone

One source node into many containers → clones for all but the last match. Plan for that when reusing widgets.

svg

SVG is not officially supported

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

Browser Support

.append() has been part of jQuery since 1.0. The callback form arrived in 1.4. It works wherever your loaded jQuery version runs.

✓ Stable · jQuery 1.0+

jQuery .append()

Supported in all browsers that run jQuery 1.0+ (callback since 1.4). Use a current jQuery 3.x build for modern projects.

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

Bottom line: Safe for any project that already loads jQuery. Prefer .text() or sanitized HTML when content comes from users.

Performance Notes

ApproachCostBest for
One .append() with a fragmentFewer reflowsMany nodes at once
Loop of tiny .append() callsMore DOM workOccasional single inserts
.empty().append(…)Clear + insertRebuilding a list from data

For large lists, build a string or DocumentFragment, then append once. Avoid appending inside a tight loop when hundreds of nodes are involved.

Key Takeaways

  • Role: insert content as the last child inside matched elements.
  • Twin: .appendTo() does the same job with reversed syntax.
  • Move: one target moves a live node; many targets clone.
  • Safety: never append untrusted HTML strings.

One line: .append() adds content at the end inside a container — use it for lists, logs, and dynamic UI growth.

Frequently Asked Questions

.append() inserts content as the last child of each matched element. It accepts HTML strings, DOM elements, text nodes, arrays, jQuery objects, or a callback (since 1.4). Returns the same jQuery collection for chaining. 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').
.append() inserts as the last child. .prepend() inserts as the first child. Use append for bottom-of-list adds; use prepend for newest-first feeds.
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.
Yes. Pass several HTML strings, DOM nodes, or jQuery objects: $('body').append($div1, newdiv2, existingEl). They insert in order as last children.
Since jQuery 1.4, yes. .append(function(index, html)) runs once per matched element. Return the content to insert. Inside the function, this is the current DOM element.
Do not append untrusted HTML strings — script tags and event attributes can run (XSS). Prefer .text() for plain text, or escape/sanitize HTML before inserting.

Next: .appendTo()

Insert matched elements into a target with reversed, content-first syntax.

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