Whenever you pass a string of HTML to any of jQuery's methods, this is what happens:

A temporary element is created, let's call it x. x's innerHTML is set to the string of HTML that you've passed. Then jQuery will transfer each of the produced nodes (that is, x's childNodes) over to a newly created document fragment, which it will then cache for next time. It will then return the fragment's childNodes as a fresh DOM collection.

Note that it's actually a lot more complicated than that, as jQuery does a bunch of cross-browser checks and various other optimisations. E.g. if you pass just <div></div> to jQuery(), jQuery will take a shortcut and simply do document.createElement('div').

EDIT: To see the sheer quantity of checks that jQuery performs, have a look here, here and here.


innerHTML is generally the faster approach, although don't let that govern what you do all the time. jQuery's approach isn't quite as simple as element.innerHTML = ... -- as I mentioned, there are a bunch of checks and optimisations occurring.


The correct technique depends heavily on the situation. If you want to create a large number of identical elements, then the last thing you want to do is create a massive loop, creating a new jQuery object on every iteration. E.g. the quickest way to create 100 divs with jQuery:

jQuery(Array(101).join('<div></div>'));

There are also issues of readability and maintenance to take into account.

This:

$('<div id="' + someID + '" class="foobar">' + content + '</div>');

... is a lot harder to maintain than this:

$('<div/>', {
    id: someID,
    className: 'foobar',
    html: content
});
Answer from James on Stack Overflow
🌐
jQuery
api.jquery.com › append
.append() | jQuery API Documentation
Appends a jQuery object (similar to an Array of DOM Elements) to all paragraphs.
🌐
W3Schools
w3schools.com › jquery › html_append.asp
jQuery append() Method
jQuery Overview jQuery Selectors jQuery Events jQuery Effects jQuery HTML/CSS jQuery Traversing jQuery AJAX jQuery Misc jQuery Properties ... The append() method inserts specified content at the end of the selected elements.
Top answer
1 of 8
341

Whenever you pass a string of HTML to any of jQuery's methods, this is what happens:

A temporary element is created, let's call it x. x's innerHTML is set to the string of HTML that you've passed. Then jQuery will transfer each of the produced nodes (that is, x's childNodes) over to a newly created document fragment, which it will then cache for next time. It will then return the fragment's childNodes as a fresh DOM collection.

Note that it's actually a lot more complicated than that, as jQuery does a bunch of cross-browser checks and various other optimisations. E.g. if you pass just <div></div> to jQuery(), jQuery will take a shortcut and simply do document.createElement('div').

EDIT: To see the sheer quantity of checks that jQuery performs, have a look here, here and here.


innerHTML is generally the faster approach, although don't let that govern what you do all the time. jQuery's approach isn't quite as simple as element.innerHTML = ... -- as I mentioned, there are a bunch of checks and optimisations occurring.


The correct technique depends heavily on the situation. If you want to create a large number of identical elements, then the last thing you want to do is create a massive loop, creating a new jQuery object on every iteration. E.g. the quickest way to create 100 divs with jQuery:

jQuery(Array(101).join('<div></div>'));

There are also issues of readability and maintenance to take into account.

This:

$('<div id="' + someID + '" class="foobar">' + content + '</div>');

... is a lot harder to maintain than this:

$('<div/>', {
    id: someID,
    className: 'foobar',
    html: content
});
2 of 8
83

They are not the same. The first one replaces the HTML without creating another jQuery object first. The second creates an additional jQuery wrapper for the second div, then appends it to the first.

One jQuery Wrapper (per example):

$("#myDiv").html('<div id="mySecondDiv"></div>');

$("#myDiv").append('<div id="mySecondDiv"></div>');

Two jQuery Wrappers (per example):

var mySecondDiv=$('<div id="mySecondDiv"></div>');
$('#myDiv').html(mySecondDiv);

var mySecondDiv=$('<div id="mySecondDiv"></div>');
$('#myDiv').append(mySecondDiv);

You have a few different use cases going on. If you want to replace the content, .html is a great call since its the equivalent of innerHTML = "...". However, if you just want to append content, the extra $() wrapper set is unneeded.

Only use two wrappers if you need to manipulate the added div later on. Even in that case, you still might only need to use one:

var mySecondDiv = $("<div id='mySecondDiv'></div>").appendTo("#myDiv");
// other code here
mySecondDiv.hide();
🌐
jQuery
api.jquery.com › after
.after() | jQuery API Documentation
Similar to other content-adding methods such as .prepend() and .before(), .after() also supports passing in multiple arguments as input. Supported input includes DOM elements, jQuery objects, HTML strings, and arrays of DOM elements.
🌐
jQuery
api.jquery.com › insertAfter
.insertAfter() | jQuery API Documentation
A selector, element, array of elements, HTML string, or jQuery object; the matched set of elements will be inserted after the element(s) specified by this parameter.
🌐
jQuery
api.jquery.com › appendTo
.appendTo() | jQuery API Documentation
With .append(), the selector expression preceding the method is the container into which the content is inserted.
🌐
Bennadel
bennadel.com › blog › 3454-jquerys-append-methods-intercept-script-tag-insertion-and-circumvent-load-handlers.htm
jQuery's append() Methods Intercept Script Tag Insertion And Circumvent Load Handlers
April 22, 2020 - So, this morning, I dug into the jQuery source code to figure out where I was going wrong. And, what I discovered is that the jQuery append methods (append, appendTo, prepend, prependTo, etc.) all intercept Script Tag insertion into the DOM tree, thereby circumventing "load" event handlers.
Find elsewhere
🌐
jQuery
api.jquery.com › prepend
.prepend() | jQuery API Documentation
The .prepend() method inserts the specified content as the first child of each element in the jQuery collection (To insert it as the last child, use .append()).
🌐
IONOS
ionos.com › digital guide › websites › web development › jquery append
How to insert elements into DOM tree with jQuery.append()
October 29, 2024 - In this example, jQuery.append() element appends three <p> tags (“First element”, “Second element” and “Third element”) si­mul­ta­ne­ous­ly to the end of div with the ID container.
🌐
W3Schools
w3schools.com › jquery › jquery_dom_add.asp
jQuery Add Elements
The jQuery append() method inserts content AT THE END of the selected HTML elements.
🌐
GeeksforGeeks
geeksforgeeks.org › jquery › jquery-append-method
jQuery append() Method - GeeksforGeeks
October 27, 2022 - The append() method in jQuery is used to insert some content at the end of the selected elements.
🌐
Peter Coles
mrcoles.com › append-array-jquery-elements
Append array of jQuery elements - Peter Coles
November 12, 2015 - The best way to append an array of jQuery elements to another in the DOM is by simply passing the array to jQuery.append:
🌐
Medium
medium.com › @jennylaister › jquery-append-vs-appendto-aa53f29c5eb3
Medium
May 29, 2017 - The .append() method allows you to insert an element into the content, which would be your container that holds all your HTML elements. The new element will be added to the end of each element that matches the selected element.
🌐
Medium
medium.com › @crushkrishkk2704 › append-child-in-jquery-a0d3985cd869
Append Child In Jquery - Krishna Kumar - Medium
February 16, 2023 - // Select the parent element var parent = $('#parent'); // Create a child element var child = $('<div>Child element</div>'); // Append the child element to the parent element parent.append(child);
🌐
Career Karma
careerkarma.com › blog › javascript › jquery methods: append()
jQuery Methods: append() | Career Karma
February 9, 2021 - The append() method is able to take one or multiple arguments of “content” (which can be an HTML string, text, an array, an element, or more jQuery), and attach or “append” it to the end of any or all elements matching the selector. Some common uses include adding an item to the end of a list (such as a new “to do” item in a to do list), or displaying new content at the click of a button.
🌐
W3Schools
w3schools.com › js › js_comments.asp
JavaScript Comments
JavaScript comments can be used to explain JavaScript code, and to make it more readable.
🌐
CSS-Tricks
css-tricks.com
CSS-Tricks - Learning for front-end designers and developers
CSS-Tricks is where folks around the world come to learn about front-end code and everything related to it, including HTML, CSS, and yes, CSS.
🌐
PortSwigger
portswigger.net › web security academy › cross-site scripting › dom-based
What is DOM-based XSS (cross-site scripting)? Tutorial & Examples | Web Security Academy
When the iframe is loaded, an XSS vector is appended to the hash, causing the hashchange event to fire. Even newer versions of jQuery can still be vulnerable via the $() selector sink, provided you have full control over its input from a source that doesn't require a # prefix.
🌐
GeeksforGeeks
geeksforgeeks.org › jquery › how-to-append-an-element-in-two-seconds-using-jquery
How to append an element in two seconds using jQuery ? - GeeksforGeeks
July 12, 2025 - Example 1: In this example, the <div> element is appended to the <body> element using append() method.
🌐
Codecademy
codecademy.com › forum_questions › 519e8e6eb1e7fe4c7200117f
Make sure to use the .append() jQuery function to add your <p> to the body of your HTML document. | Codecademy
In normal situation, you wouldnt have to include the <p></p> because jQuery will add the <p></p> as it’s intended to do. The actual code in the script.js is the following: (document).ready(function(){$("body").append("<p>"I'm a paragraph"</p>");}); Hope this helps!