eateElement() - Web APIs - MDN
Maybe your like
- Skip to main content
- Skip to search
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
- Learn more
- See full compatibility
- Report feedback
In an HTML document, the document.createElement() method creates the HTML element specified by localName, or an HTMLUnknownElement if localName isn't recognized.
In this article
- Syntax
- Examples
- Specifications
- Browser compatibility
- See also
Syntax
jscreateElement(localName) createElement(localName, options)Parameters
localNameA string that specifies the type of element to be created. Don't use qualified names (like "html:a") with this method. When called on an HTML document, createElement() converts localName to lower case before creating the element. In Firefox, Opera, and Chrome, createElement(null) works like createElement("null").
options OptionalAn object with the following properties:
isThe tag name of a custom element previously defined via customElements.define(). See Web component example for more details.
Return value
The new Element.
Note: A new HTMLElement is returned if the document is an HTMLDocument, which is the most common case. Otherwise a new Element is returned.
Examples
Basic example
This creates a new <div> and inserts it before the element with the ID div1.
HTML
html<!doctype html> <html lang="en-US"> <head> <meta charset="UTF-8" /> <title>Working with elements</title> </head> <body> <div id="div1">The text above has been created dynamically.</div> </body> </html>JavaScript
jsfunction addElement() { // create a new div element const newDiv = document.createElement("div"); // and give it some content const newContent = document.createTextNode("Hi there and greetings!"); // add the text node to the newly created div newDiv.appendChild(newContent); // add the newly created element and its content into the DOM const currentDiv = document.getElementById("div1"); document.body.insertBefore(newDiv, currentDiv); } addElement();Result
Web component example
Note: Check the browser compatibility section for support, and the is attribute reference for caveats on implementation reality of customized built-in elements.
The following example snippet is taken from our expanding-list-web-component example (see it live also). In this case, our custom element extends the HTMLUListElement, which represents the <ul> element.
js// Create a class for the element class ExpandingList extends HTMLUListElement { constructor() { // Always call super first in constructor super(); // constructor definition left out for brevity // … } } // Define the new element customElements.define("expanding-list", ExpandingList, { extends: "ul" });If we wanted to create an instance of this element programmatically, we'd use a call along the following lines:
jslet expandingList = document.createElement("ul", { is: "expanding-list" });The new element will be given an is attribute whose value is the custom element's tag name.
Note: For backwards compatibility, some browsers will allow you to pass a string here instead of an object, where the string's value is the custom element's tag name.
Specifications
| Specification |
|---|
| DOM# ref-for-dom-document-createelement① |
Browser compatibility
See also
- Node.removeChild()
- Node.replaceChild()
- Node.appendChild()
- Node.insertBefore()
- Node.hasChildNodes()
- document.createElementNS() — to explicitly specify the namespace URI for the element.
Help improve MDN
Was this page helpful to you? Yes No Learn how to contributeThis page was last modified on Sep 11, 2025 by MDN contributors.
View this page on GitHub • Report a problem with this content Filter sidebar- Document Object Model (DOM)
- Document
- Constructor
- Document()
- Instance properties
- activeElement
- activeViewTransition
- adoptedStyleSheets
- alinkColor Deprecated
- all Deprecated
- anchors Deprecated
- applets Deprecated
- bgColor Deprecated
- body
- characterSet
- childElementCount
- children
- compatMode
- contentType
- cookie
- currentScript
- defaultView
- designMode
- dir
- doctype
- documentElement
- documentURI
- domain Deprecated
- embeds
- featurePolicy Experimental
- fgColor Deprecated
- firstElementChild
- fonts
- forms
- fragmentDirective
- fullscreen Deprecated
- fullscreenElement
- fullscreenEnabled
- head
- hidden
- images
- implementation
- lastElementChild
- lastModified
- lastStyleSheetSet Non-standard Deprecated
- linkColor Deprecated
- links
- location
- pictureInPictureElement
- pictureInPictureEnabled
- plugins
- pointerLockElement
- preferredStyleSheetSet Non-standard Deprecated
- prerendering Experimental
- readyState
- referrer
- rootElement Deprecated
- scripts
- scrollingElement
- selectedStyleSheetSet Non-standard Deprecated
- styleSheets
- styleSheetSets Non-standard Deprecated
- timeline
- title
- URL
- visibilityState
- vlinkColor Deprecated
- xmlEncoding Deprecated
- xmlVersion Deprecated
- Static methods
- parseHTML() Experimental
- parseHTMLUnsafe()
- Instance methods
- adoptNode()
- append()
- ariaNotify() Experimental Non-standard
- browsingTopics() Non-standard Deprecated
- caretPositionFromPoint()
- caretRangeFromPoint() Non-standard
- clear() Deprecated
- close()
- createAttribute()
- createAttributeNS()
- createCDATASection()
- createComment()
- createDocumentFragment()
- createElement()
- createElementNS()
- createEvent() Deprecated
- createExpression()
- createNodeIterator()
- createNSResolver() Deprecated
- createProcessingInstruction()
- createRange()
- createTextNode()
- createTouch() Non-standard Deprecated
- createTouchList() Non-standard Deprecated
- createTreeWalker()
- elementFromPoint()
- elementsFromPoint()
- enableStyleSheetsForSet() Non-standard Deprecated
- evaluate()
- execCommand() Deprecated
- exitFullscreen()
- exitPictureInPicture()
- exitPointerLock()
- getAnimations()
- getElementById()
- getElementsByClassName()
- getElementsByName()
- getElementsByTagName()
- getElementsByTagNameNS()
- getSelection()
- hasFocus()
- hasPrivateToken() Experimental
- hasRedemptionRecord() Experimental
- hasStorageAccess()
- hasUnpartitionedCookieAccess()
- importNode()
- moveBefore()
- mozSetImageElement() Non-standard
- open()
- prepend()
- queryCommandEnabled() Non-standard Deprecated
- queryCommandState() Non-standard Deprecated
- queryCommandSupported() Non-standard Deprecated
- querySelector()
- querySelectorAll()
- releaseCapture() Non-standard
- replaceChildren()
- requestStorageAccess()
- requestStorageAccessFor() Experimental
- startViewTransition()
- write() Deprecated
- writeln() Deprecated
- Events
- afterscriptexecute Non-standard Deprecated
- beforescriptexecute Non-standard Deprecated
- DOMContentLoaded
- fullscreenchange
- fullscreenerror
- pointerlockchange
- pointerlockerror
- prerenderingchange Experimental
- readystatechange
- scroll
- scrollend
- scrollsnapchange Experimental
- scrollsnapchanging Experimental
- securitypolicyviolation
- selectionchange
- visibilitychange
- Inheritance
- Node
- EventTarget
- Related pages for DOM
- AbortController
- AbortSignal
- AbstractRange
- Attr
- CDATASection
- CharacterData
- Comment
- CustomEvent
- DOMError Deprecated
- DOMException
- DOMImplementation
- DOMParser
- DOMTokenList
- DocumentFragment
- DocumentType
- Element
- Event
- EventTarget
- HTMLCollection
- MutationObserver
- MutationRecord
- NamedNodeMap
- Node
- NodeIterator
- NodeList
- ProcessingInstruction
- QuotaExceededError Experimental
- Range
- ShadowRoot
- StaticRange
- Text
- TreeWalker
- XMLDocument
- XPathEvaluator
- XPathExpression
- XPathResult
- XSLTProcessor
- Guides
- Anatomy of the DOM
- Attribute reflection
- Selection and traversal on the DOM tree
- Building and updating the DOM tree
- Working with events
Tag » Add Div Javascript
-
How To Add A New Element To HTML DOM In JavaScript?
-
HTML DOM Element AppendChild() Method - W3Schools
-
HTML DOM Document CreateElement() Method - W3Schools
-
How Can I Create And Style A Div Using JavaScript? - Stack Overflow
-
Element.append() - Web APIs | MDN - Mozilla
-
How To Add A Div In Javascript Code Example - Code Grepper
-
How To Insert A DIV Block And Other HTML Elements Into A Web ...
-
How To Append HTML Code To A Div Using JavaScript - GeeksforGeeks
-
Dynamically Create A
Copyright © 2022 | Designer Truyền Hình Cáp Sông Thu