Skip to main content

createElement (W3C DOM Core method)

By Adam Roberts



Free JavaScript Book!

Write powerful, clean and maintainable JavaScript.

RRP $11.95



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

    ('The man who mistook his wife for a hat'));

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

The end result of that operation would be this HTML:

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


tagname (DOMString)

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.


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

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

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.

New books out now!

Get practical advice to start your career in programming!

Master complex transitions, transformations and animations in CSS!

Latest Remote Jobs