document.createElement() is an instance method that creates a new element with the tag name you pass (see MDN Document: createElement()). Learn the lowercase HTML rule, how to set content and attributes, insert nodes with appendChild / insertBefore, the optional options object for customized built-ins, how it compares to createElementNS(), and five try-it labs.
01
Kind
Instance method
02
Arg
localName
03
Returns
HTMLElement
04
Attach
append / insert
05
Name rule
lowercase
06
Status
Baseline
Fundamentals
Introduction
Static HTML is great for the first paint. Dynamic pages need JavaScript to build buttons, cards, list items, and alerts after the page loads.
document.createElement(localName) is the standard way to create those nodes. You get a real DOM element — not an HTML string — then set its text, attributes, and listeners before attaching it to the tree.
💡
Create → configure → attach
1) const el = document.createElement("div") 2) Set textContent, className, id, listeners 3) parent.appendChild(el) (or append / insertBefore)
Prefer this over building markup with string concatenation and innerHTML when you control structure and text separately — it avoids accidental HTML injection and keeps nodes easy to update.
localName — string for the element type. Do not use qualified names like "html:a" (MDN). On HTML documents the name is lowercased. In Firefox, Opera, and Chrome, createElement(null) behaves like createElement("null") (MDN).
options(optional) — object. Only one of is or customElementRegistry may be set (MDN):
is — tag name of a customized built-in defined with customElements.define(..., { extends: "..." }); the element gets an is attribute.
customElementRegistry — a CustomElementRegistry for a scoped custom element registry.
Return value
The new Element. For an HTMLDocument (the common case), MDN: a new HTMLElement is returned.
Exceptions
InvalidCharacterError — localName is not a valid element name (MDN).
NotSupportedError — both is and customElementRegistry are specified (MDN).
MDN basic pattern
JavaScript
const newDiv = document.createElement("div");
const newContent = document.createTextNode("Hi there and greetings!");
newDiv.appendChild(newContent);
const currentDiv = document.getElementById("div1");
document.body.insertBefore(newDiv, currentDiv);
expanding-list
true
(in browsers that support customized built-in elements)
How It Works
The first argument is still the built-in tag ("ul"). The is option links it to the custom definition and sets the is attribute (MDN). Some browsers historically allowed a string instead of an options object for compatibility.
Applications
🚀 Common Use Cases
Dynamic UI — build cards, rows, toasts, and form controls from data.
Safer text — pair with textContent instead of untrusted HTML strings.
Event wiring — attach listeners on the element object before or after insert.
Lists and tables — create many children, optionally via a DocumentFragment.
Custom elements — create autonomous tags or customized built-ins (where supported).
Not for SVG tags in HTML — use createElementNS for SVG/MathML namespaces.
🧠 How createElement() Works
1
Pass localName
MDN: a tag name string; on HTML documents it is converted to lowercase.
Input
2
Browser builds a node
Returns an HTMLElement, or HTMLUnknownElement if unrecognized.
Create
3
Configure the element
Set text, attributes, classes, and event listeners while detached.
Setup
4
✓
Insert into the DOM
Use appendChild, append, or insertBefore so users can see it.
Important
📝 Notes
MDN: Baseline Widely available since July 2015 (* some parts, like options, may vary).
Do not pass qualified names such as "html:a" to createElement (MDN).
Document.createElement() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project. Optional features such as customized built-ins (options.is) may have varying support.
✓ Baseline Widely available
Document.createElement()
Create HTML elements in every major browser — the foundation of dynamic DOM UI.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
createElement()Wide
Bottom line: Use createElement for HTML nodes, createElementNS for SVG/MathML, and always append the node so it appears in the page.
Wrap Up
Conclusion
document.createElement(localName) creates a detached HTML element. Configure it with properties and listeners, then insert it with appendChild, append, or insertBefore. That three-step habit is the core of building dynamic pages without unsafe HTML strings.
Inject untrusted HTML with innerHTML when createElement + textContent suffices
Set both is and customElementRegistry
Use createElement("svg") when you need a real SVG namespace element
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about createElement()
Builds real DOM elements for dynamic UI.
5
Core concepts
📝01
Returns
HTMLElement
MDN
🔗02
Attach
append / insert
step 3
📄03
Name
lowercase
HTML
⚡04
Prefer
textContent
safe
🛡05
Status
Baseline
2015
❓ Frequently Asked Questions
MDN: Document.createElement() creates a new element with the given localName (tag name). On an HTML document it returns an HTMLElement (or HTMLUnknownElement if the tag is not recognized).
No. MDN marks Document.createElement() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. Some optional features (like customized built-ins via options.is) have varying support.
A new Element. For the usual HTML document case, MDN says an HTMLElement is returned. Unknown tag names yield HTMLUnknownElement.
Yes on HTML documents (MDN). createElement() converts localName to lowercase before creating the element. Do not pass qualified names like "html:a".
Use document.createElementNS(namespaceURI, qualifiedName) when you need an explicit namespace — for example SVG (http://www.w3.org/2000/svg) or MathML. Prefer createElement() for normal HTML tags.
createElement() only builds a detached node. Attach it with appendChild(), append(), prepend(), insertBefore(), or replaceChild() on a parent that is already in the document.
Did you know?
MDN notes that creating an element does not put it on the page by itself. Beginners often call createElement, set textContent, and wonder why nothing appears — until they remember appendChild or append. The node lives in memory first; insertion makes it visible.