YouTip LogoYouTip

Dom Element

```html XML DOM - Element Object

Element Object


Element Object

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

Note: Text is always stored in text nodes. A common mistake in DOM processing is to navigate to an element node and assume it contains text. However, even the simplest element node has a text node underneath it. For example, in <year>2005</year>, there is an element node (year), and beneath this element node there is a text node 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 element's absolute base URI.
childNodes Returns a NodeList of the element's child nodes.
firstChild Returns the element's first child node.
lastChild Returns the element's last child node.
localName Returns the local part of the element's name.
namespaceURI Returns the element's namespace URI.
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) to which the element belongs.
parentNode Returns the element's parent node.
prefix Sets or returns the element's namespace prefix.
previousSibling Returns the node immediately preceding the element.
schemaTypeInfo Returns the type information associated with the element.
tagName Returns the element's name.
textContent Sets or returns the textual 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 a node.
cloneNode() Clones a 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 an attribute node as an Attribute object.
getAttributeNodeNS() Returns an attribute node as an Attribute object (with namespace).
getElementsByTagName() Returns a NodeList of matching elements and their descendants.
getElementsByTagNameNS() Returns a NodeList of matching elements (with namespace) and their descendants.
getFeature(feature,version) Returns a DOM object that implements the specialized APIs of the specified feature and version.
getUserData(key) Returns the object associated with the key on this node. The object must first have been set to this node by calling setUserData with the same key.
hasAttribute() Returns whether the element has an attribute with the specified name.
hasAttributeNS() Returns whether the element has an attribute 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 namespace.
isEqualNode() Checks if two nodes are equal.
isSameNode() Checks if two nodes are the same node.
isSupported(feature,version) Returns whether the specified feature is supported on this element.
lookupNamespaceURI() Returns the namespace URI associated with the given prefix.
lookupPrefix() Returns the prefix associated with the given namespace URI.
normalize() Normalizes the node by putting all Text nodes underneath it (including attribute nodes) 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 a specified attribute.
removeAttributeNS() Removes a 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 with a key on this 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, 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, 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, this method declares the specified attribute to be a user-determined ID attribute.
```
← Dom AttributeDom Processinginstruction β†’