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:
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.
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
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
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 = $();
tableEl = $table[];
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(.());
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>")
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.
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 or ?
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.
CSS :not() Selector for Excluding a Class or Attribute
Learn how to use the CSS :not() selector to target elements that do not have a specific class or attribute, with examples and common mistakes.
Can HTML Checkboxes Be Readonly? Understanding readonly vs disabled in HTML Forms
Learn why HTML checkboxes do not support readonly, how disabled differs, and practical ways to prevent changes while still submitting values.