createElement (W3C DOM Core method)
- 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