
- HTML - Home
- HTML - Roadmap
- HTML - Introduction
- HTML - History & Evolution
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Headings
- HTML - Paragraphs
- HTML - Fonts
- HTML - Blocks
- HTML - Style Sheet
- HTML - Formatting
- HTML - Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM createElement() Method
The HTML DOM createElement() method is used for creating an HTML element specified by tag name or element name, if the tag name is not recognized then an HTML Unknown Element is created.
The following interactive example demonstrate the usage of the createElement() method for different scenarios −
- If you click the "Create p Element" button, a new <p> element will be created.
- If you click the "Create List" button, a new "list" element will be created.
- If you click the "Create Button Element" button, a new "button" element will be created.
Syntax
Following is the syntax of the HTML DOM createElement() method −
document.createElement(tagname);
Parameters
This method accepts a single parameter as listed below −
Parameter | Description |
---|---|
tagname | It is a required parameter which represents the type of element to be created. |
Return Value
It returns the newly created element node.
Example 1: Create "Button" and "hr" Element
The following example demonstrates the usage of the HTML DOM createElement() method. It creates the "button" and "hr" elements within the document −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM createElement() Method</title> <style> button{ padding: 10px; } </style> </head> <body> <p>Click to get more buttons</p> <button onclick="fun()">Click me</button><br><br> <script> let i = 0; function fun() { i++; let btn = document.createElement("button"); btn.innerHTML = "button" + i; let hr = document.createElement("hr"); document.body.appendChild(btn); document.body.appendChild(hr); } </script> </body> </html>
Example 2: Create Paragraph ("p") Element
Following is another example of the HTML DOM createElement() method. We use this method to create a "p" element and then append it to the <body> −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM createElement() Method</title> <style> button{ padding: 10px; } </style> </head> <body> <p>Click to get more Paragraphs</p> <button onclick="fun()">Click me</button><br><br> <script> let i = 0; function fun() { i++; let txt = document.createElement("p"); txt.innerHTML = "I am Paragraph number " + i; document.body.appendChild(txt); } </script> </body> </html>
Example 3: Create Paragraph ("p") Element and append to <div>
In the following example, a <p> element is created using the createElement() method, and later it is appended to<div>element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM createElement() Method</title> <style> button{ padding: 10px; } </style> </head> <body> <p>Click to get more Paragraphs</p> <button onclick="fun()">Click me</button><br><br> <div id="ids"></div> <script> let i = 0; function fun() { i++; let txt = document.createElement("p"); txt.innerHTML = "I am Paragraph number " + i; document.getElementById("ids").appendChild(txt); } </script> </body> </html>
Supported Browsers
Method | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
createElement() | Yes 1 | Yes 12 | Yes 1 | Yes 1 | Yes 6 |