HTML DOM Document createElement() method
- Previous page createDocumentFragment()
- Next Page createEvent()
- Go to the Previous Level HTML DOM Documents
Definition and usage
createElement()
The method creates an element node.
See also:
Example
Example 1
Create a <p> element and attach it to the document:
const para = document.createElement("p"); para.innerText = "This is a paragraph"; document.body.appendChild(para);
Example 2
Create a <p> element and attach it to the element:
const para = document.createElement("p"); para.innerHTML = "This is a paragraph."; document.getElementById("myDIV").appendChild(para);
Example 3
Create a button:
const btn = document.createElement("button"); btn.innerHTML = "Hello Button"; document.body.appendChild(btn);
Syntax
document.createElement(tagName)
Parameter
Parameter | Description |
---|---|
tagName |
Required. The tag name of the element to be created. HTML element names can be in any case. XML element names need to be case-sensitive. |
Return value
Type | Description |
---|---|
Node | The newly created element node has the specified tag name. |
throw
If the tagName contains illegal characters, this method will throw an error code of INVALID_CHARACTER_ERR
of the DOMException exception.
Browser support
document.createElement()
It is a feature of DOM Level 1 (1998).
It is supported by all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | 9-11 | Support | Support | Support | Support |
- Previous page createDocumentFragment()
- Next Page createEvent()
- Go to the Previous Level HTML DOM Documents