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
A function that returns an HTML string, DOM element(s), text node(s), or jQuery object to insert at the end of each element in the set of matched elements. Receives the index position of the element in the set and the old HTML value of the element as arguments. Within the function, this refers to the current element in the set. The .append() method inserts the specified content as the last child of each element in the jQuery collection ...
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();
🌐
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.
🌐
jQuery
api.jquery.com › after
.after() | jQuery API Documentation
HTML string, DOM element, text node, array of elements and text nodes, or jQuery object to insert after each element in the set of matched elements.
🌐
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. The callback function is called for each added object and outputs the HTML content of the re­spec­tive element and the cor­re­spond­ing index to the console.
🌐
jQuery
api.jquery.com › prepend
.prepend() | jQuery API Documentation
One or more additional DOM elements, text nodes, arrays of elements and text nodes, HTML strings, or jQuery objects to insert at the beginning of each element in the set of matched elements.
🌐
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 › 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.
🌐
W3Schools
w3schools.com › jquery › jquery_dom_add.asp
jQuery Add Elements
$("p").append("Some appended text."); Try it Yourself » · The jQuery prepend() method inserts content AT THE BEGINNING of the selected HTML elements.
🌐
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 the index.html you have add to the following paragraph tags <p></p> between the body tags; There is something funky happening with the code editor. 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!
🌐
Bootstrap
getbootstrap.com › docs › 4.0 › components › dropdowns
Dropdowns · Bootstrap
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-offset="".
🌐
W3Schools
w3schools.com › python › python_lists_add.asp
Python - Add List Items
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
🌐
UNPKG
unpkg.com
UNPKG
If you don't specify a file path, UNPKG will resolve the file based on the package's default entry point. In many packages that are meant solely for frontend development, like jQuery and GSAP, this will be the value of the main field in the package.json file.
🌐
GeeksforGeeks
geeksforgeeks.org › jquery › jquery-appendto-method
jQuery appendTo() Method - GeeksforGeeks
July 11, 2025 - The appendTo() method is an inbuilt method in jQuery that is used to insert HTML element at the end of the selected element.
🌐
W3Schools
w3schools.com › c › c_files_write.php
C Write To Files
HTML Certificate CSS Certificate JavaScript Certificate Front End Certificate SQL Certificate Python Certificate PHP Certificate jQuery Certificate Java Certificate C++ Certificate C# Certificate XML Certificate
🌐
Fambuilt
fambuilt.org › 7tq3
AntiBot Cloud: скрипт для защиты сайтов на php от плохих ботов.
This process is automatic. Your browser will redirect to your requested content shortly · Please allow up to 1 seconds
🌐
W3Schools
w3schools.com › cpp › cpp_strings_concat.asp
C++ String Concatenation
HTML Certificate CSS Certificate JavaScript Certificate Front End Certificate SQL Certificate Python Certificate PHP Certificate jQuery Certificate Java Certificate C++ Certificate C# Certificate XML Certificate
🌐
jQuery
api.jquery.com › appendTo
.appendTo() | jQuery API Documentation
A selector, element, HTML string, array of elements, or jQuery object; the matched set of elements will be inserted at the end of the element(s) specified by this parameter. The .append() and .appendTo() methods perform the same task. The major difference is in the syntax-specifically, in the placement of the content and target.
🌐
W3Schools
w3schools.com › js › js_array_methods.asp
JavaScript Array Methods
The length property returns the length (size) of an array.
🌐
Cloudflare
developers.cloudflare.com › turnstile › get-started › client-side-rendering
Embed the widget · Cloudflare Turnstile docs
June 17, 2026 - Turnstile offers two ways to add widgets to your page. Implicit rendering automatically scans your HTML for widget containers when the page loads. Explicit rendering gives you programmatic control to create widgets at any time using JavaScript. Use implicit rendering for static pages where forms exist at page load.