Here's your example in the "one" line.

this.$OuterDiv = $('<div></div>')
    .hide()
    .append($('<table></table>')
        .attr({ cellSpacing : 0 })
        .addClass("text")
    )
;

Update: I thought I'd update this post since it still gets quite a bit of traffic. In the comments below there's some discussion about $("<div>") vs $("<div></div>") vs $(document.createElement('div')) as a way of creating new elements, and which is "best".

I put together a small benchmark, and here are roughly the results of repeating the above options 100,000 times:

jQuery 1.4, 1.5, 1.6

               Chrome 11  Firefox 4   IE9
<div>            440ms      640ms    460ms
<div></div>      420ms      650ms    480ms
createElement    100ms      180ms    300ms

jQuery 1.3

                Chrome 11
<div>             770ms
<div></div>      3800ms
createElement     100ms

jQuery 1.2

                Chrome 11
<div>            3500ms
<div></div>      3500ms
createElement     100ms

I think it's no big surprise, but document.createElement is the fastest method. Of course, before you go off and start refactoring your entire codebase, remember that the differences we're talking about here (in all but the archaic versions of jQuery) equate to about an extra 3 milliseconds per thousand elements.


Update 2

Updated for jQuery 1.7.2 and put the benchmark on JSBen.ch which is probably a bit more scientific than my primitive benchmarks, plus it can be crowdsourced now!

http://jsben.ch/#/ARUtz

Answer from nickf on Stack Overflow
Top answer
1 of 15
1376

Here's your example in the "one" line.

this.$OuterDiv = $('<div></div>')
    .hide()
    .append($('<table></table>')
        .attr({ cellSpacing : 0 })
        .addClass("text")
    )
;

Update: I thought I'd update this post since it still gets quite a bit of traffic. In the comments below there's some discussion about $("<div>") vs $("<div></div>") vs $(document.createElement('div')) as a way of creating new elements, and which is "best".

I put together a small benchmark, and here are roughly the results of repeating the above options 100,000 times:

jQuery 1.4, 1.5, 1.6

               Chrome 11  Firefox 4   IE9
<div>            440ms      640ms    460ms
<div></div>      420ms      650ms    480ms
createElement    100ms      180ms    300ms

jQuery 1.3

                Chrome 11
<div>             770ms
<div></div>      3800ms
createElement     100ms

jQuery 1.2

                Chrome 11
<div>            3500ms
<div></div>      3500ms
createElement     100ms

I think it's no big surprise, but document.createElement is the fastest method. Of course, before you go off and start refactoring your entire codebase, remember that the differences we're talking about here (in all but the archaic versions of jQuery) equate to about an extra 3 milliseconds per thousand elements.


Update 2

Updated for jQuery 1.7.2 and put the benchmark on JSBen.ch which is probably a bit more scientific than my primitive benchmarks, plus it can be crowdsourced now!

http://jsben.ch/#/ARUtz

2 of 15
153

Simply supplying the HTML of elements you want to add to a jQuery constructor $() will return a jQuery object from newly built HTML, suitable for being appended into the DOM using jQuery's append() method.

For example:

var t = $("<table cellspacing='0' class='text'></table>");
$.append(t);

You could then populate this table programmatically, if you wished.

This gives you the ability to specify any arbitrary HTML you like, including class names or other attributes, which you might find more concise than using createElement and then setting attributes like cellSpacing and className via JS.

๐ŸŒ
W3Schools
w3schools.com โ€บ jquery โ€บ jquery_dom_add.asp
jQuery Add Elements
However, both the append() and prepend() methods can take an infinite number of new elements as parameters. The new elements can be generated with text/HTML (like we have done in the examples above), with jQuery, or with JavaScript code and DOM elements. In the following example, we create several ...
๐ŸŒ
MakeUseOf
makeuseof.com โ€บ home โ€บ programming โ€บ learn how to create an element in jquery
Learn How to Create an Element in jQuery
March 30, 2021 - An element is the behind-the-scenes object that represents the marked-up text. Think of an element as the DOM counterpart to the HTML tags. Like most jQuery operations, creating an element starts with the dollar function, $().
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ jquery โ€บ what-is-the-equivalent-method-of-document-createelement-in-jquery
What is the equivalent method of document.createElement() in jQuery ? - GeeksforGeeks
August 5, 2025 - In jQuery, there are several ways to create new HTML elements dynamically. One of the common ways is to use the $() method to create new elements, but there is also a method that is equivalent to the native document.createElement() method.
๐ŸŒ
jQuery
api.jquery.com โ€บ add
.add() | jQuery API Documentation
Using an HTML snippet as the .add() method's argument (as in the third version), we can create additional elements on the fly and add those elements to the matched set of elements. Let's say, for example, that we want to alter the background of the list items along with a newly created paragraph: Although the new paragraph has been created and its background color changed, it still does not appear on the page. To place it on the page, we could add one of the insertion methods to the chain. As of jQuery 1.4 the results from .add() will always be returned in document order (rather than a simple concatenation).
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ jquery โ€บ jquery create element
How to Create Elements in jQuery | Delft Stack
February 2, 2024 - The after() method will create an element after the given element, and similarly, the before() method will create an element before the given element. ... <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jque...
๐ŸŒ
Hongkiat
hongkiat.com โ€บ home โ€บ how to create and insert new elements with jquery (part 1)
How to Create and Insert New Elements with jQuery (Part 1) - Hongkiat
April 3, 2025 - This will help you appreciate the simplicity that jQuery offers. To add an element to a document in JavaScript, you first need to create it. You can use the .createElement() function for this purpose.
Find elsewhere
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ jquery create element
jQuery create element
May 24, 2023 - When you pass a string containing HTML as the first parameter, this function will create a new element: ... $("<element>"): The dollar sign ($) followed by parentheses is the jQuery function that creates a new element.
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-create-a-div-element-in-jquery.php
How to Create a DIV Element in jQuery
You can simply use the append() method to dynamically create an element such as a <div> at the end of the selected element in jQuery. Similarly, you can use the prepend() to create or insert element at the beginning.
๐ŸŒ
Codemia
codemia.io โ€บ home โ€บ knowledge hub โ€บ jquery document.createelement equivalent?
jQuery document.createElement equivalent? | Codemia
January 2, 2025 - The jQuery equivalent of document.createElement("div") is usually $("<div>"). jQuery creates the element, wraps it in a jQuery object, and lets you chain attribute, content, and insertion methods in one expression. Native DOM code creates an element first and configures it afterward: javascript ยท
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ jquery โ€บ jquery-create-a-div-element
Create a Div Element using jQuery - GeeksforGeeks
January 29, 2024 - We can create a div element with the following steps: Create a new <div> element.
๐ŸŒ
Clubmate
clubmate.fi โ€บ different-methods-to-make-dom-elements-in-jquery
Different methods to make DOM elements in jQuery โ€“ clubmate.fi
This was added in jQuery 1.4. var $el = $('<a/>', { id: 'link2', class: 'link', href: 'http://example.com', title: 'This is a test', rel: 'external', text: 'Testing' }) This resonates positively in my programmer monkey brain. According to JsPerf the following method is also the fastest (if that's important to you). var $e = $(document.createElement('div')) $e.addClass('foo module') .attr('name', 'bob') .text('This div is created with jQuery and inserted here.') .insertAfter('#module')
๐ŸŒ
Edureka Community
edureka.co โ€บ home โ€บ community โ€บ categories โ€บ web development โ€บ java-script โ€บ how to create html elements using jquery
How to create HTML elements using jQuery | Edureka Community
September 18, 2020 - The method that I used to create the new elements on the page has overwhelmingly been ... create elements in jQuery from a performance perspective?
๐ŸŒ
jQuery
api.jquery.com โ€บ jQuery
jQuery( selector [, context ] )Returns
You can perform any of the usual jQuery methods on this object: For explicit parsing of a string to HTML, use the $.parseHTML() method. By default, elements are created with an .ownerDocument matching the document into which the jQuery library was loaded.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ jquery โ€บ how-to-create-a-div-element-in-jquery
How to create a div element in jQuery ? - GeeksforGeeks
July 11, 2025 - <!DOCTYPE html> <html> <head> ... <body> <div class="initial"> This is initial div </div> </html> Output: Method 2: Using appendTo() Method: The appendTo() method in jQuery is used to insert HTML element at the end of the selected ...