Home to Document Object Model | Up to Core
Prev NamedNodeMap | Next Attr


Node
Object

This object represents a single node within the document tree. As such, it provides methods for dealing with the tree hierarchy, including handling child nodes. Be aware that not all objects that inherit from Node actually permit child nodes (e.g. Text nodes). Further, it provides "standard" attributes of nodeName, nodeValue and attributes which may or may not map to the specific object's attributes (e.g. Comment nodes do not have attributes). For details of a particular Node type, see the relevant write up:

  1. Element
  2. Attr
  3. Text
  4. CDATASection *
  5. EntityReference *
  6. Entity *
  7. ProcessingInstruction *
  8. Comment
  9. Document
  10. DocumentType *
  11. DocumentFragment
  12. Notation
* - extended interface present if DOMImplementation.hasFeature("XML", "2.0") is true.

The number against each entry above corresponds to its nodeType value. Values up to 200 are reserved.

attributes
Attribute (read only, NamedNodeMap)

As of DOM2, only Element nodes have attributes.

childNodes
Attribute (read only, NodeList)

The NodeList returned is null if there are no child nodes.

firstChild
Attribute (read only, Node)

Null if there are no child nodes.

lastChild
Attribute (read only, Node)

Null if there are no child nodes.

localName
Attribute (read only, string)

Introduced in DOM Level 2. See namespaceURI.

namespaceURI
Attribute (read only, string)

Introduced in DOM Level 2. Null if there is no namespace specified. The value is the literal value used when the node was created, not resolved in any way. Under DOM2, only Element and Attribute nodes have namespaces and only then when created with the new methods.

nextSibling
Attribute (read only, Node)

The node following this one at the current level of the hierarchy. Null if this is the last node beneath its parent.

nodeName
Attribute (read only, DOMString)

The value of nodeName is dependent upon the nodeType. Individual write ups will indicate the actual value.

nodeType
Attribute (read only, unsigned short)

See above for a list of current node types and their type numbers. The DOM implementation probably provides symbolic constants (e.g. "DOCUMENT_TYPE_NODE") for these values - use them.

nodeValue
Attribute (DOMString; can throw DOMException on setting and retrieval)

The content of nodeValue is dependent upon the nodeType. Individual write ups will indicate what is returned.

The exceptions thrown are:

NO_MODIFICATION_ALLOWED_ERR
The node is read only.
DOMSTRING_SIZE_ERR
An attempt was made to exceed the (implementation defined) maximum string length.

ownerDocument
Attribute (read only, Document)

Modified in DOM Level 2. The document object that "owns" this node, i.e. that was used to create it. Note that document nodes and DocumentType nodes that are not in use have null ownerDocuments.

parentNode
Attribute (read only, Node)

This node's "parent" within the hierarchy. Null if the node hasn't been added to or has been removed from the hierarchy. (Always null for Attribute, Document, DocumentFragment, Entity and Notation nodes.) When null, nextSibling and previousSibling are null.

prefix
Attribute (string; can throw DOMException on setting)

Introduced in DOM Level 2. See namespaceURI.

The exceptions thrown are:

INVALID_CHARACTER_ERR
An attempt was made to set a prefix containing an illegal character.
NO_MODIFICATION_ALLOWED_ERR
The node is read only.
NAMESPACE_ERR
The namespace prefix is invalid.

previousSibling
Attribute (read only, Node)

The node preceeding this one at the current level of the hierarchy. Null if this is the first node beneath its parent.

appendChild
Method
ECMAScript binding: appendChild(newChild) (returns Node; newChild is a Node; can raise DOMException)

Functionally equivalent to insertBefore(newChild, null).

The exceptions thrown are:

HIERARCHY_REQUEST_ERR
A circular reference would be created or this node does not permit child nodes of newChild's or its decendents' type.
WRONG_DOCUMENT_ERR
newChild does not belong in this document.
NO_MODIFICATION_ALLOWED_ERR
The node is read only.

cloneNode
Method
ECMAScript binding: cloneNode(deep) (returns Node; deep is boolean)

Returns a copy of this node. The copy's parentNode is null. Child nodes copied are:

  • If this is an Element node, the Attributes are copied.
  • If deep is true, all child nodes are copied.

There are other, subtle effects of cloneNode:

Note that cloning an immutable subtree results in a mutable copy, but the children of an EntityReference clone are readonly. In addition, clones of unspecified Attr nodes are specified.

hasAttributes
Method
ECMAScript binding: hasAttributes() (returns boolean)

Introduced in DOM Level 2. In DOM2, returns false unless this is an Entity node that is parent to one or more Attribute nodes.

hasChildNodes
Method
ECMAScript binding: hasChildNodes() (returns boolean)

Returns false until this node is parent to one or more nodes.

insertBefore
Method
ECMAScript binding: insertBefore(newChild, refChild) (returns Node; newChild and refChild are Nodes; can raise DOMException)

If newChild is already a child of this node, it is removed. If refChild is not null, the insertion point is positioned before it. If null, then the insertion point is after the current last child node. newChild is then added at the insertion point.

If newChild is a DocumentFragment, all the contained nodes are added to this node's children.

The exceptions thrown are:

HIERARCHY_REQUEST_ERR
A circular reference would be created or this node does not permit child nodes of newChild's or its decendents' type.
WRONG_DOCUMENT_ERR
newChild does not belong in this document.
NO_MODIFICATION_ALLOWED_ERR
The node is read only or newChild is read only (we need to set its parentNode!).
NOT_FOUND_ERR
refChild (which is not null) cannot be found as a child of this node.

isSupported
Method
ECMAScript binding: isSupported(feature, version) (returns boolean; feature and version are strings)

Modified in DOM Level 2. Checks whether a feature (assumed to pass the hasFeature check on the DOMImplementation) is supported by this node. The parameters are the same as hasFeature.

normalize
Method
ECMAScript binding: normalize() (no return value)

Merges all logically adjacent Text nodes.

removeChild
Method
ECMAScript binding: removeChild(oldChild) (returns Node; oldChild is a Node; can raise DOMException)

oldChild is removed from this node's children (and returned).

The exceptions thrown are:

NO_MODIFICATION_ALLOWED_ERR
The node is read only or newChild is read only (we need to set its parentNode!).
NOT_FOUND_ERR
refChild (which is not null) cannot be found as a child of this node.

replaceChild
Method
ECMAScript binding: replaceChild(newChild, oldChild) (returns Node; newChild and oldChild are Nodes; can raise DOMException)

If newChild is already a child of this node, it is removed. oldChild is removed (and returned) and newChild is added in its place. If newChild is a DocumentFragment, all the contained nodes are added to this node's children.

The exceptions thrown are:

HIERARCHY_REQUEST_ERR
A circular reference would be created or this node does not permit child nodes of newChild's or its decendents' type.
WRONG_DOCUMENT_ERR
newChild does not belong in this document.
NO_MODIFICATION_ALLOWED_ERR
The node is read only or newChild is read only (we need to set its parentNode!).
NOT_FOUND_ERR
oldChild cannot be found as a child of this node.