Easy Tutorial
❮ Dom Nodes Nodelist Home ❯

XML DOM - Element Object


Element Object

The Element object represents an element in an XML document. Elements can contain attributes, other elements, or text. If an element contains text, the text is represented in a text node.

Important: Text is always stored in text nodes. A common mistake in DOM processing is navigating to an element node and expecting the node to contain text. However, even the simplest element node has a text node beneath it. For example, in <year>2005</year>, there is an element node (year) and a text node beneath it containing the text (2005).

Since the Element object is also a node, it inherits the properties and methods of the Node object.

Element Object Properties

Property Description
attributes Returns a NamedNodeMap of the element's attributes.
baseURI Returns the absolute base URI of the element.
childNodes Returns a NodeList of the element's child nodes.
firstChild Returns the first child node of the element.
lastChild Returns the last child node of the element.
localName Returns the local part of the element's name.
namespaceURI Returns the namespace URI of the element.
nextSibling Returns the node immediately following the element.
nodeName Returns the name of the node, depending on its type.
nodeType Returns the type of the node.
ownerDocument Returns the root element (document object) the element belongs to.
parentNode Returns the parent node of the element.
prefix Sets or returns the namespace prefix of the element.
previousSibling Returns the node immediately preceding the element.
schemaTypeInfo Returns the type information associated with the element.
tagName Returns the name of the element.
textContent Sets or returns the text content of the element and its descendants.

Element Object Methods

Method Description
appendChild() Adds a new child node to the end of the list of children of the node.
cloneNode() Clones the node.
compareDocumentPosition() Compares the document position of two nodes.
getAttribute() Returns the value of an attribute.
getAttributeNS() Returns the value of an attribute (with namespace).
getAttributeNode() Returns the attribute node as an Attribute object.
getAttributeNodeNS() Returns the attribute node as an Attribute object (with namespace).
getElementsByTagName() Returns a NodeList of elements with a given tag name and their descendants.
getElementsByTagNameNS() Returns a NodeList of elements with a given tag name (with namespace) and their descendants.
getFeature(feature, version) Returns a DOM object that implements the specialized API of the specified feature and version.
getUserData(key) Returns the object associated with the key on the node. This object must first be set on this node by calling setUserData with the same key.
hasAttribute() Returns whether the element has any attributes with the specified name.
hasAttributeNS() Returns whether the element has any attributes with the specified name and namespace.
hasAttributes() Returns whether the element has any attributes.
hasChildNodes() Returns whether the element has any child nodes.
insertBefore() Inserts a new child node before an existing child node.
isDefaultNamespace(URI) Returns whether the specified namespaceURI is the default.
isEqualNode() Checks whether two nodes are equal.
isSameNode() Checks whether two nodes are the same node.
isSupported(feature, version) Returns whether the specified feature is supported on this element.
lookupNamespaceURI() Returns the namespace URI that matches the specified prefix.
lookupPrefix() Returns the prefix that matches the specified namespace URI.
normalize() Puts all text nodes in the full depth of the sub-tree underneath the node into a "normal" form where only structure (e.g., elements, comments, processing instructions, CDATA sections, and entity references) separates Text nodes, i.e., there are neither adjacent Text nodes nor empty Text nodes.
removeAttribute() Removes the specified attribute.
removeAttributeNS() Removes the specified attribute (with namespace).
removeAttributeNode() Removes the specified attribute node.
removeChild() Removes a child node.
replaceChild() Replaces a child node.
setUserData(key, data, handler) Associates an object to a key on the element.
setAttribute() Adds a new attribute.
setAttributeNS() Adds a new attribute (with namespace).
setAttributeNode() Adds a new attribute node.
setAttributeNodeNS(attrnode) Adds a new attribute node (with namespace).
setIdAttribute(name, isId) If the isId attribute of the Attribute object is true, then this method declares the specified attribute to be a user-determined ID attribute.
setIdAttributeNS(uri, name, isId) If the isId attribute of the Attribute object is true, then this method declares the specified attribute to be a user-determined ID attribute (with namespace).
setIdAttributeNode(idAttr, isId) If the isId attribute of the Attribute object is true, then this method declares the specified attribute to be a user-determined ID attribute.
❮ Dom Nodes Nodelist Home ❯