Skip to main content

XML DOM: 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.

note

Text is always stored in text nodes!

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

Properties

PropertyDescription
attributesReturns a NamedNodeMap of attributes for the element
baseURIReturns the absolute base URI of the element
childNodesReturns a NodeList of child nodes for the element
firstChildReturns the first child of the element
lastChildReturns the last child of the element
localNameReturns the local part of the name of the element
namespaceURIReturns the namespace URI of the element
nextSiblingReturns the node immediately following the element
nodeNameReturns the name of the node, depending on its type
nodeTypeReturns the type of the node
ownerDocumentReturns the root element (document object) for an element
`parentNodeReturns the parent node of the element
prefixSets or returns the namespace prefix of the element
previousSiblingReturns the node immediately before the element
schemaTypeInfoReturns the type information associated with the element
tagNameReturns the name of the element
textContentSets or returns the text content of the element and its descendants

Methods

MethodDescription
appendChild()Adds a new child node to the end of the list of children of the 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 a namespace)
getAttributeNode()Returns an attribute node as an Attribute object
getAttributeNodeNS()Returns an attribute node (with a namespace) as an Attribute object
getElementsByTagName()Returns a NodeList of matching element nodes, and their children
getElementsByTagNameNS()Returns a NodeList of matching element nodes (with a namespace), and their children
getFeature(feature,version)Returns a DOM object which implements the specialized APIs of the specified feature and version
getUserData(key)Returns the object associated to a key on a this node. The object must first have been set to this node by calling setUserData with the same key
hasAttribute()Returns whether an element has any attributes matching a specified name
hasAttributeNS()Returns whether an element has any attributes matching a 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 if two nodes are equal
lookupNamespaceURI()Returns the namespace URI matching a specified prefix
lookupPrefix()Returns the prefix matching a specified namespace URI
normalize()Puts all text nodes underneath this element (including attributes) 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 a namespace)
removeAttributeNode()Removes a 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 a namespace)
setAttributeNode()Adds a new attribute node
setAttributeNodeNS(attrnode)Adds a new attribute node (with a namespace)
setIdAttribute(name,isId)If the isId property 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 property of the Attribute object is true, this method declares the specified attribute (with a namespace) to be a user-determined ID attribute
setIdAttributeNode(idAttr,isId)If the isId property of the Attribute object is true, this method declares the specified attribute to be a user-determined ID attribute

Table of Contents