Question
jQuery Element Creation and DOM Manipulation vs document.createElement
Question
I'm refactoring older JavaScript code that does a lot of manual DOM manipulation using the native DOM API. For example:
var d = document;
var odv = d.createElement("div");
odv.style.display = "none";
this.OuterDiv = odv;
var t = d.createElement("table");
t.cellSpacing = 0;
t.className = "text";
odv.appendChild(t);
I want to know whether there is a cleaner or better way to write this using jQuery.
I tried something like this:
var odv = $.create("div");
$.append(odv);
but I am not sure whether that is valid jQuery or whether it is actually better than using document.createElement() directly.
Short Answer
By the end of this page, you'll understand how jQuery creates DOM elements, how that differs from native document.createElement(), and when each approach is appropriate. You'll also see how to set attributes, styles, classes, and append elements in a cleaner way using practical examples.
Concept
When you build HTML elements in JavaScript, you are working with the DOM (Document Object Model). The native browser API provides methods like document.createElement() and appendChild() for this.
jQuery offers a different style: instead of calling low-level DOM methods directly, you usually create elements by passing an HTML-like string to $(), then chain methods such as .addClass(), .css(), .append(), and .appendTo().
For example, native DOM code like this:
var div = document.createElement("div");
div.className = "box";
div.style.display = "none";
can be written in jQuery like this:
var $div = $("<div>")
.addClass("box")
.css("display", "none");
This matters because:
- jQuery can make DOM code shorter and more readable.
- jQuery provides a consistent API for common tasks.
- Chaining often reduces repetitive variable access.
- Older codebases that already use jQuery often prefer jQuery-style DOM manipulation for consistency.
However, jQuery is not automatically better in every situation. Native DOM methods are built into the browser, are straightforward, and in modern code are often preferred when jQuery is not otherwise needed.
A key point for this question: there is no standard jQuery method called $.create(). In jQuery, element creation is normally done with:
$("<tag>")
So if you want the jQuery equivalent of document.createElement("div"), the usual pattern is:
var $div = $("<div>");
Mental Model
Think of native DOM manipulation as building furniture using individual tools:
document.createElement()= cut a new piece- setting properties = paint or label it
appendChild()= attach it to the structure
jQuery is more like using a toolkit with a smoother workflow:
$("<div>")= create the piece.addClass()/.css()= style it.append()/.appendTo()= place it where it belongs
Both approaches build the same thing. jQuery mainly changes how you express the steps, often making the code feel more fluent.
Syntax and Examples
Native DOM version:
var d = document;
var odv = d.createElement("div");
odv.style.display = "none";
var t = d.createElement("table");
t.cellSpacing = 0;
t.className = "text";
odv.appendChild(t);
A jQuery version:
var $odv = $("<div>").css("display", "none");
var $table = $("<table>", {
cellspacing: 0,
"class": "text"
});
$odv.append($table);
this.OuterDiv = $odv[0];
What this does
$("<div>")creates a newdivelement..css("display", "none")sets the inline style.$("<table>", { ... })creates atableand sets properties/attributes.$odv.append($table)appends the table inside the div.$odv[0]gets the raw DOM element if you need to store it like the original code.
Another readable jQuery style
var $odv = $("<div>");
$odv.hide();
var $table = $("<table>")
.attr("cellspacing", 0)
.addClass("text");
$odv.append($table);
If you want the final HTML inserted into the page
$("body").append($odv);
This appends the newly created div to the page body.
Step by Step Execution
Consider this jQuery example:
var $box = $("<div>").hide();
var $table = $("<table>").attr("cellspacing", 0).addClass("text");
$box.append($table);
$("body").append($box);
Step by step:
-
$("<div>")- jQuery creates a new
divelement in memory. - It is not yet on the page.
- jQuery creates a new
-
.hide()- jQuery sets the element's display so it is hidden.
-
$("<table>")- jQuery creates a new
tableelement. - This table is also only in memory at this point.
- jQuery creates a new
-
.attr("cellspacing", 0)- Adds the
cellspacingattribute to the table.
- Adds the
-
.addClass("text")- Adds the CSS class
textto the table.
- Adds the CSS class
-
$box.append($table)- Places the table inside the div.
- The structure now exists in memory as:
<div style="display: none;"> <table cellspacing="0" class="text"></table> </div> -
$("body").append($box)- Appends the whole div to the actual document.
- Now it exists on the visible page, although hidden because of
display: none.
This is important: creating an element and appending it are separate steps in both native DOM and jQuery.
Real World Use Cases
Creating elements dynamically is common in real applications.
Rendering API data
If an API returns a list of products, you might create rows, cards, or table entries using jQuery.
var $row = $("<tr>");
$row.append($("<td>").text(product.name));
$row.append($("<td>").text(product.price));
Building reusable UI components
Older jQuery-based codebases often construct modals, tooltips, dropdowns, and alerts dynamically.
var $alert = $("<div>")
.addClass("alert")
.text("Saved successfully");
Conditional rendering
You may create elements only when needed.
if (items.length === 0) {
$("#results").append($("<p>").text("No results found"));
}
Data tables and forms
Dynamic forms often create inputs, labels, rows, and buttons based on configuration.
var $input = $("<input>", {
type: "text",
name: "username"
});
Real Codebase Usage
In real projects, developers usually choose one style and stay consistent.
Common jQuery patterns
Create and configure in one chain
var $button = $("<button>")
.addClass("save-btn")
.text("Save")
.prop("disabled", true);
Append multiple child elements
var $card = $("<div>").addClass("card");
$card.append($("<h2>").text("Profile"));
$card.append($("<p>").text("User details go here"));
Use variables with $ prefix
Many codebases name jQuery objects like $table or $dialog to distinguish them from raw DOM elements.
var $table = $("<table>");
var tableEl = $table[0];
Convert between jQuery objects and DOM nodes
- jQuery object to DOM node:
$el[0] - DOM node to jQuery object:
$(el)
This is common when older code mixes jQuery and native APIs.
Use text safely instead of string concatenation
$("<p>").text(userInput);
This avoids accidentally inserting HTML when plain text is intended.
When developers avoid jQuery here
In modern codebases, developers often use native DOM methods instead, especially if jQuery is not already a dependency. But in legacy jQuery-heavy applications, using jQuery for element creation can improve consistency and readability.
Common Mistakes
1. Assuming $.create() exists
This is not a standard jQuery API.
Broken code:
var el = $.create("div");
Correct:
var $el = $("<div>");
2. Confusing a jQuery object with a DOM element
Broken code:
var $div = $("<div>");
$div.appendChild(document.createElement("span"));
appendChild() is a DOM method, not a jQuery method.
Correct:
var $div = $("<div>");
$div.append($("<span>"));
Or use the DOM node:
$div[0].appendChild(document.createElement("span"));
3. Using .attr() when .prop() is more appropriate
For many DOM properties like checked or disabled, .prop() is usually the better choice.
$("<input>").prop("disabled", true);
4. Forgetting that created elements are not yet in the document
Broken assumption:
var $div = $("<div>");
// expecting it to already appear on the page
You still need to append it somewhere:
$("body").append($div);
5. Mixing raw HTML strings carelessly
This can become hard to read:
var $el = $("<div class='box'><table class='text'></table></div>");
This may be valid, but chaining methods is often clearer when structure gets more complex.
6. Losing track of what this.OuterDiv should store
If old code expects a DOM node, do this:
this.OuterDiv = $odv[0];
If the rest of the code expects a jQuery object, store the jQuery object instead:
this.$outerDiv = $odv;
Comparisons
| Approach | Example | Best for | Notes |
|---|---|---|---|
| Native DOM | document.createElement("div") | Modern vanilla JS, no jQuery dependency | Direct and built-in |
| jQuery creation | $("<div>") | jQuery-based projects | Easy chaining |
| Native append | parent.appendChild(child) | Raw DOM workflows | Works with DOM nodes only |
| jQuery append | $parent.append($child) | jQuery workflows | Accepts jQuery objects, DOM nodes, or HTML strings |
document.createElement() vs $("<tag>")
| Feature | Native DOM | jQuery |
|---|---|---|
| Create element | document.createElement("div") | $("<div>") |
| Add class | el.className = "box" | $el.addClass("box") |
| Set style | el.style.display = "none" | $el.css("display", "none") or $el.hide() |
| Append child | parent.appendChild(child) | $parent.append($child) |
| Return type | DOM element | jQuery object |
Which should you use?
- Use jQuery if the codebase already heavily uses jQuery.
- Use native DOM if you are writing modern JavaScript without jQuery.
- Do not switch styles randomly unless needed.
Cheat Sheet
// Create element with jQuery
var $div = $("<div>");
// Set class
$div.addClass("box");
// Set CSS
$div.css("display", "none");
$div.hide(); // shorthand for hiding
// Set attributes
var $table = $("<table>").attr("cellspacing", 0);
// Set properties
var $input = $("<input>").prop("disabled", true);
// Set text
var $p = $("<p>").text("Hello");
// Append child
$div.append($table);
// Append to document
$("body").append($div);
// Convert jQuery object to DOM node
var divEl = $div[0];
// Convert DOM node to jQuery object
var $wrapped = $(divEl);
Key rules
- There is no standard
$.create()in jQuery. - Use
$("<tag>")to create new elements. - jQuery methods work on jQuery objects, not raw DOM nodes.
- Use
$el[0]when you need the real DOM element. - Creating an element does not insert it into the page.
FAQ
Is $.create() a real jQuery function?
No. Standard jQuery does not include $.create(). Use $("<div>") to create elements.
What is the jQuery equivalent of document.createElement()?
Usually $("<tag>"), such as $("<div>") or $("<table>").
Is jQuery better than document.createElement()?
Not always. jQuery can be shorter and more consistent in older jQuery projects, but native DOM methods are perfectly valid and common in modern JavaScript.
How do I append a jQuery-created element to the page?
Use .append() or .appendTo().
$("body").append($("<div>"));
How do I get the actual DOM element from a jQuery object?
Use index access:
var el = $div[0];
Should I use .attr() or .prop()?
Use .attr() for HTML attributes and .prop() for DOM properties like checked, selected, or disabled.
Can I mix jQuery and native DOM code?
Yes, but be careful. Know whether your variable holds a DOM node or a jQuery object.
Mini Project
Description
Build a small message panel dynamically using jQuery. This project demonstrates how to create elements, apply classes and styles, add text, nest child elements, and append the final result to the page. It reflects the same skills used when refactoring older DOM-heavy JavaScript code.
Goal
Create a hidden message panel with a heading, a table, and a button, then insert it into the page and show it when the button is clicked.
Requirements
Create the panel using jQuery element creation syntax. Add a heading and a table inside the panel. Hide the panel initially. Append the panel to the page. Add a button that shows the panel when clicked.
Keep learning
Related questions
Allow Only Numeric Input (0-9) in HTML Input Using jQuery
Learn how to allow only digits 0-9 in an HTML input using jQuery, with examples, validation tips, common mistakes, and best practices.
Apply CSS !important with jQuery .css()
Learn why jQuery .css() cannot set !important and how to use CSSStyleDeclaration.setProperty() to apply inline important styles safely.
CSS ::before and ::after on Input Elements
Learn why CSS ::before and ::after usually do not work on input elements, what replaced elements are, and practical alternatives.