Browser/User Agent Support
| IE | Mozilla | Netscape | Opera | Safari | Netscapte | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
|---|
Constructors
| Constructor | IE | Mozilla | Netscape | Opera | Safari | Netscapte |
|---|---|---|---|---|---|---|
Node in an HTML document tree. | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
Properties
| Property | IE | Mozilla | Netscape | Opera | Safari | Netscapte |
|---|---|---|---|---|---|---|
Attributes for an element node. | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
Child nodes of the current node. | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
First child node of the current node. | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
Last child node of the current node. | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
Local part of an element or attribute name if it the node was defined with an XML Namespace. | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
URI of the namespace for an element or attribute node if the node was defined with an XML Namespace. | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
Sibling node immediately after the current node. | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
Name of the node. Same as tag name for element nodes. | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
Type of node. See Remarks for valid values. | 5.5+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
| ||||||
Value of the current node. | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
Document object that contains this node. | 6.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
Parent node of the current node. | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
Namespace prefix for an element or attribute node if the node was defined with an XML Namespace. | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
Sibling node immediately before the current node. | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
Methods
| Method | IE | Mozilla | Netscape | Opera | Safari | Netscapte |
|---|---|---|---|---|---|---|
Appends a node to the childNodes array for the node. | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
| ||||||
Creates a copy of either the node or the node and its children. | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
| ||||||
Returns true if the node is an element node with attributes. | no | 1.0+ | 6.0+ | 7.0+ | 1.3+ | no |
Returns true if the node has child nodes. | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
Inserts a node into the childNodes array for the node before the specified child node. | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
Returns true if the specified feature and version are supported. | no | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
Merges text nodes adjacent to the element to create a normalized DOM. | N/A | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
| ||||||
Removes the specified child node from the element and returns the node. | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
Removes the specified child node, replaces it with another node, and returns the removed node. | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
Remarks
This object represents a unique node within a DOM tree. It is the lowest-level unit for representing the contents
of an XML document, from which more specific types are derived. Perhaps the most commonly used derivitives of
Node are Element, Attribute and Text.
You will never actually create a node directly as it is intended to be a basic interface for more specific classes, but nevertheless the properties and methods defined here are available in many different DOM objects.
References
Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
Constructor Detail
Property Detail
NamedNodeMap attributes - read only
Attributes for an element node.
-
For examples, see the quirksmode test page:
- Remarks
-
This property returns a listing of all the attributes bound to this
Node. Since onlyElementnodes support attributes, mostNodetypes will return a null value for this property.NamedNodeMaps are essentially the DOM-equivilant of a hash. This property provides an alternative to the specificElement.getAttribute andElement.hasAttribute methods, which are usually used when you already know which attributes an element has. For those circumstances when you want to do some automatic discovery of attributes and their values, this property provides a convenient way to iterate through the element's attributes.This property also has the added benefit of being usable across all nodes, not just
Elementnodes. Therefore no checking needs to be done ahead of time to verify if a node is anElement; rather you can iterate over theattributes list, since any element that has no attributes, or any other DOM node, will have a null value for this property. - Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
NodeList childNodes - read only
Child nodes of the current node.
-
For examples, see the quirksmode test page:
- Remarks
-
The list of nodes that are immediate children of this node. The DOM is a heirarchial structure of
Nodeobjects. This property, combined with theparentNode properties provide the basics for defining this structure. Several convenience properties are provided for accessing some frequently-used children nodes, namely thefirstChild andlastChild properties.Since the
NodeListclass can be referenced as an array as well as its object-oriented interface, one can easily iterate through the contents of a node using standard JavaScriptforloops. Because this property is read-only, you cannot alter theNodeListto effect this node's contents. Instead, the methods for addeding and removing nodes should be used, which will result in this being updated automatically. - Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
Node firstChild - read only
First child node of the current node.
-
For examples, see the quirksmode test page:
- Remarks
-
Returns the first child node from the list of the current node's children. If this node doesn't have any children, a null value will be returned.
This is functionally equivilant to invoking
node., though this method is much more suscinct and doesn't require any error trapping if the child node doesn't exist.childNodes .NodeList.item (0) - Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
Node lastChild - read only
Last child node of the current node.
-
For examples, see the quirksmode test page:
- Remarks
-
Returns the last child node from the list of the current node's children. If this node doesn't have any children, a null value will be returned.
This is functionally equivilant to invoking
node., though this method is much more suscinct and doesn't require any error trapping if the child node doesn't exist.childNodes .NodeList.item (node.childNodes .NodeList.length - 1) - Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
String localName - read only
Local part of an element or attribute name if it the node was defined with an XML Namespace.
- Availability
HTML DOM Level 2 | W3C
String namespaceURI - read only
URI of the namespace for an element or attribute node if the node was defined with an XML Namespace.
- Availability
HTML DOM Level 2 | W3C
Node nextSibling - read only
Sibling node immediately after the current node.
-
For examples, see the quirksmode test page:
- Remarks
-
Returns the next node to the current one within the parent node's children. If this node is the last child within it's parent, then a null value will be returned.
When working with a node, it is often useful to be able to manipulate or search the nodes surrounding it. For instance, if given a node representing a label, you might want to find the text node immediately next to it. To help with this, the
nextSibling property allows you to traverse to neighboring nodes in the DOM. - Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
String nodeName - read only
Name of the node. Same as tag name for element nodes.
-
For examples, see the quirksmode test page:
- Remarks
-
This returns a name that represents this node's name. Depending on the type of object this is, this property will either return the name identifying this object; for instance, if this node is a
ElementorAttrobject, then the tag or attribute name is returned. In other cases some place-holder text will be returned.nodeName node-type valuesType left Value left AttrAttribute name CDATASection#cdata-sectionComment#commentDocument#documentDocumentFragment#document-fragmentDocumentTypeDocument type name ElementTag name EntityEntity name EntityReferenceName of entity referenced NotationNotation name ProcessingInstructionTarget Text#textIf
XML Namespaces were defined for this node and this node is aElementorAttrobject (since these are the only node types that support the use ofnamespaces ), this property returns the node name including theprefix andlocalName .Returns an upper case tag name.
- Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
Number nodeType - read only
Type of node. See Remarks for valid values.
-
For examples, see the quirksmode test page:
- Remarks
-
This property returns an integer indicating what type of DOM
Nodethis object represents.Because the
Nodeclass is inherited by several other, more specific DOM objects, this property is a programmatic way of determining what an arbitrary node is, and therefore can tell the programmer how to interact with it. There are a series of constants defined in this object that can be used to refer to this property:nodeList constant valuesValue right Constant left 1ELEMENT_NODE 2ATTRIBUTE_NODE 3TEXT_NODE 4CDATA_SECTION_NODE 5ENTITY_REFERENCE_NODE 6ENTITY_NODE 7PROCESSING_INSTRUCTION_NODE 8COMMENT_NODE 9DOCUMENT_NODE 10DOCUMENT_TYPE_NODE 11DOCUMENT_FRAGMENT_NODE 12NOTATION_NODE - Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
String nodeValue - read only
Value of the current node.
-
For examples, see the quirksmode test page:
- Remarks
-
This represents the value of the node. Only
Attr,CDATASection,Comment,ProcessingInstruction, andTextobjects can contain a value in this property. For all other types of objects this property will return null, and setting it to a different value has no effect. - Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
Document ownerDocument - read only
Document object that contains this node.
-
For examples, see the quirksmode test page:
- Remarks
-
Refers to the
Documentobject that this node exists in. Because a node must be created as a child of aDocument, and sinceNodeobjects cannot be moved arbitrarily from one document to another (not without duplicating it), this is a way of referring to the parent document for a node so that document-wide method calls can be used for a node. - Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
Node parentNode - read only
Parent node of the current node.
-
For examples, see the quirksmode test page:
- Remarks
-
This property contains a reference to the parent node for the current node. Since the DOM is a heirarchial structure of nodes, the
parentNode andchildNodes properties tie the collection of nodes together.Not all nodes can have parents however.
Attr,Document,DocumentFragment,EntityandNotationobjects and as such will contain a null value for this property. As well this property will be null for newly-created nodes that have yet to be added to a location within the DOM tree. - Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
String prefix - read only
Namespace prefix for an element or attribute node if the node was defined with an XML Namespace.
- Availability
HTML DOM Level 2 | W3C
Node previousSibling - read only
Sibling node immediately before the current node.
-
For examples, see the quirksmode test page:
- Remarks
- Returns the previous node to the current one within the parent node's children. If this node is the first child within it's parent, then a null value will be returned.
- Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
Method Detail
appendChild(Node newChild) : static Node
Appends a node to the childNodes array for the node.
| Node | newChild | Node to add to the current element as a child. |
-
Using appendChild
var newElement = document.Document.createElement('label'); newElement.Element.setAttribute('value', 'Username:'); var usernameText = document.Document.getElementById('username'); usernameText.appendChild(newElement);For more examples, see the quirksmode test page:
- Remarks
-
Add the provided node at the end of this node's children. The newly added node is returned when the operation is completed. There are a few interesting caveats with this method.
If the node supplied to this method already exists somewhere in the DOM tree, then it is first removed before being appended. As well, if the value supplied to this method is a
DocumentFragment, then the entire contents of the fragment are moved to the end of this node's children. - Throws
- Raises a HIERARCHY_REQUEST_ERR error if either the supplied node is one of this node's ancestors, or if the current node doesn't allow the given type of node as a child.
- Raises a WRONG_DOCUMENT_ERR error if the supplied node doesn't exist within this node's document.
- Raises a NO_MODIFICATION_ALLOWED_ERR error if the node is read-only.
- See Also
- Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
cloneNode(Boolean deep) : static Node
Creates a copy of either the node or the node and its children.
| Boolean | deep | If true, clone the child nodes, also. |
-
Using cloneNode
var myObject = document.Document.getElementById('EmailAddress'); var newObject = myObject.cloneNode(true); newObject.id = 'ClonedEmailAddress'; myObject.parentNode.insertBefore(newObject, myObject);For more examples, see the quirksmode test page:
- Remarks
-
Because a
Nodecan only exist in one location in the DOM tree at a time, it is necessary to create and insert a new node even if you want a simple copy of an element or attribute, or any other kind of DOMNode. This method, therefore, provides a facility for making an exact duplicate of a node, capable of being inserted into the DOM tree at another point.Since
Nodes are heirarchial, the argument to this method indicates whether or not you would like this node's children cloned as well. If you passfalseinto this method, then only the properties on the node you are cloning will be duplicated.Once cloned, the new node will have no parent, requiring you to add it somewhere to the DOM tree yourself. When cloning a
Elementnode, any properties defined by the XML processor, including attributes and their defaults, will be cloned. It's important to note, however, that the text contents of anElementare in factTextnodes, and will not be cloned unless a deep clone is requested.If you clone an
Attrobject explicitly, rather than simply cloning anElement, theAttr.specified property is set to true, regardless of whether or not the attribute you are cloning from was specified.The requirements of having unique node IDs within a DOM tree still holds true, and as such any unique identifiers within the cloned nodes need to be changed before you insert them back into the DOM tree.
- Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
hasAttributes() : Boolean
Returns true if the node is an element node with attributes.
-
Using hasAttributes
var obj = document.Document.getElementById('EmailAddress'); if (obj.hasAttributes()) { window.alert("The node " + obj.Node.nodeName + " has attributes"); }For more examples, see the quirksmode test page:
- Remarks
- Returns true if this node is an
Elementand has any attributes. Returns false if there are none. Equivilant to checkingnode..attributes.NamedNodeMap.length > 0 - See Also
Element.getAttribute | Element.hasAttribute | Node.attributes
- Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
hasChildNodes() : Boolean
Returns true if the node has child nodes.
-
Using hasChildNodes
var obj = document.Document.getElementById('EmailAddress'); if (obj.hasChildNodes()) { window.alert("The node " + obj.Node.nodeName + " has children"); } - Remarks
- Returns true if this node has any child nodes. Returns false if there are none. Equivilant to checking
node..childNodes.NodeList.length> 0 - See Also
- Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
insertBefore(Node newChild, Node refChild) : static Node
Inserts a node into the childNodes array for the node before the specified child node.
| Node | newChild | Child node to insert. |
| Node | refChild | Sibling node for the new child. |
-
Using insertBefore
var obj = document.Document.getElementById('EmailAddress'); var newElement = document.Document.createElement('label'); var refElement = obj.Node.childNodes[3]; try { obj.insertBefore(newElement, refElement); } catch DOMException { // Error handling };For more examples, see the quirksmode test page:
- Remarks
-
This method is used to insert a new child element at a very specific location in amongst multiple children. By specifying as the refChild parameter one of the element's children nodes, the new node will be inserted immediately prior to it.
If the node supplied to this method already exists somewhere in the DOM tree, then it is first removed before being inserted. As well, if the value supplied to this method is a
DocumentFragment, then the entire contents of the fragment are inserted, in the same order, before therefChild reference node.. - Throws
- Raises a HIERARCHY_REQUEST_ERR error if either the supplied node is one of this node's ancestors, or if the current node doesn't allow the given type of node as a child.
- Raises a WRONG_DOCUMENT_ERR error if the supplied node doesn't exist within this node's document.
- Raises a NO_MODIFICATION_ALLOWED_ERR error if the node is read-only.
- Raises a NOT_FOUND_ERR error if the supplied
refChildnode is not a child of this one.
- See Also
- Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
isSupported(String feature, [String version]) : Boolean
Returns true if the specified feature and version are supported.
| String | feature | Name of the feature. |
| String | version | Version of the feature. (optional) |
- Remarks
- Tests whether a node supports the specified feature. Similar to the
DOMImplementation.hasFeaturemethod and takes the same feature names. If version is null, returns true if any version of the feature is supported. - See Also
- Availability
HTML DOM Level 2 | W3C
normalize() : void
Merges text nodes adjacent to the element to create a normalized DOM.
-
For examples, see the quirksmode test page:
- Remarks
-
Normalizes any
Textnodes contained under this node and all of its children. Merges any adjacentTextnodes into one object, so that the nodes reflect how they would be structured if this XML document were freshly loaded.Normalize()is useful for manipulating the text content of nodes for editing. Instead of worrying about altering existingTextnodes, you can simply add new nodes and perform thenormalizewhen you finish your updates. - Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
removeChild(Node oldChild) : static Node
Removes the specified child node from the element and returns the node.
| Node | oldChild | Child node to remove. |
-
Using removeChild
var obj = document.Document.getElementById('EmailAddress'); try { var oldChild = obj.removeChild(obj.Node.firstChild); } catch DOMException { // Error handling };For more examples, see the quirksmode test page:
- Remarks
- Removes and returns the specified child node from the list of this node's children.
- Throws
- Raises a NO_MODIFICATION_ALLOWED_ERR if the node is read-only.
- Raises a NOT_FOUND_ERR if the supplied node is not a child of this one.
- See Also
- Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
replaceChild(Node newChild, Node oldChild) : static Node
Removes the specified child node, replaces it with another node, and returns the removed node.
| Node | newChild | Node to replace the oldChild node with. |
| Node | oldChild | Node to be replaced. |
-
Using replaceChild
var obj = document.Document.getElementById('EmailAddress'); var newElement = document.Document.createElement('label'); try { var oldChild = obj.replaceChild(newElement, obj.Node.lastChild); } catch DOMException { // Error handling };For more examples, see the quirksmode test page:
- Remarks
-
This method simplifies the process of calling
removeChildand theninsertBefore, so that you do not have to worry if this node is the first or last child.The new child node is removed if it already exists somewhere else in the DOM tree. If the new child is a
DocumentFragmentnode, theoldChildnode is replaced by all of the nodes within theDocumentFragment, in the same order. - Throws
- Raises a HIERARCHY_REQUEST_ERR error if either the node being inserted is one of this node's ancestors, or if the current node does not allow this type of node as a child.
- Raises a WRONG_DOCUMENT_ERR error if the newChild node was not created within the current document.
- Raises a NO_MODIFICATION_ALLOWED_ERR if this node is read-only or if the parent of the new node is read-only.
- Raises a NOT_FOUND_ERR if the supplied oldChild node is not a child of this one.
- See Also
- Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
