createElement (W3C DOM Core method)

Share this article

Returns
Element
Throws
INVALID_CHARACTER_ERR

Example

var element = document.createElement('h1');

element.appendChild(document.createTextNode
    ('The man who mistook his wife for a hat'));

The example above creates an <h1> element, and then adds text to it.

The end result of that operation would be this HTML:

<h1>The man who mistook his wife for a hat</h1>

Arguments

tagname (DOMString) required

The tagName of the element. In XML this is case-sensitive; in HTML the name can be specified in any case, but will be converted to the canonical upper-case form of HTML tag names.

Description

Create an Element node of the specified type.

The created element can then be added to the document using Node methods such as appendChild or insertBefore.

A created element implements the Element interface as soon as it’s created, so attributes can be added to it immediately, without having to append it to the document first. If the element has default attributes in this document type, those attributes are automatically created and attached to the element.

This method creates non-namespaced elements; to create a namespaced element, use the DOM 2 “createElementNS”createElementNS method instead.

Return value

The created element node, with its nodeName set to the specified tag name, and its localName, prefix and namespaceURI set to null

Frequently Asked Questions (FAQs) about createElement W3C DOM Core Method

What is the createElement method in JavaScript?

The createElement method is a powerful tool in JavaScript that allows developers to dynamically create new elements in the Document Object Model (DOM). This method is part of the W3C DOM Core standard, which is a set of APIs for interacting with HTML and XML documents. With createElement, you can specify the type of element you want to create, such as a div, span, or img, and then append it to an existing element in the DOM.

How does the createElement method work?

The createElement method works by taking a string argument that specifies the type of element to be created. Once the element is created, it can be customized with attributes, styles, and content before being appended to an existing element in the DOM. This allows for dynamic content generation and manipulation in web applications.

Can I add attributes to an element created with createElement?

Yes, you can add attributes to an element created with createElement. After creating the element, you can use the setAttribute method to add attributes to it. This method takes two arguments: the name of the attribute and the value to be set.

How can I append an element created with createElement to the DOM?

After creating and customizing an element with createElement, you can append it to the DOM using the appendChild method. This method is called on the parent element to which you want to append the new element.

Can I use createElement with React?

Yes, you can use createElement with React. In fact, JSX syntax in React is just syntactic sugar for createElement calls. However, React’s createElement method works a bit differently than the standard DOM method. It takes three arguments: the type of element, an object containing the element’s props, and the children of the element.

Why is my createElement function not working as expected?

There could be several reasons why your createElement function is not working as expected. You might have misspelled the element type, forgotten to append the new element to the DOM, or tried to set an attribute that doesn’t exist. Make sure to check your code carefully for these common mistakes.

Can I create multiple elements at once with createElement?

While createElement itself only creates one element at a time, you can certainly use it in a loop to create multiple elements. Just make sure to append each new element to the DOM within the loop.

Can I use createElement to create custom elements?

Yes, you can use createElement to create custom elements, also known as web components. However, before you can create a custom element, you need to define it with customElements.define.

How can I add event listeners to an element created with createElement?

You can add event listeners to an element created with createElement in the same way as any other element. Use the addEventListener method, specifying the event type and the callback function to be executed when the event occurs.

Can I use createElement to replace an existing element in the DOM?

Yes, you can use createElement in combination with other DOM methods to replace an existing element. After creating the new element, use the replaceChild method on the parent of the element to be replaced, passing in the new element and the existing element as arguments.

Adam RobertsAdam Roberts
View Author

Adam is SitePoint's head of newsletters, who mainly writes Versioning, a daily newsletter covering everything new and interesting in the world of web development. He has a beard and will talk to you about beer and Star Wars, if you let him.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form