# Node : Object
Return to: HTML DOM Level 2 index

Node in an HTML document tree.

## Platform Support

IE | Mozilla | Netscape | Opera | Safari
--- | --- | --- | --- | ---
5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

## Constructors

Constructor | Action | IE | Mozilla | Netscape | Opera | Safari
--- | --- | --- | --- | --- | --- | ---
Node Constructor() : Node Node in an HTML document tree. | Show Details | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

### Node() : Node

Node in an HTML document tree.

Returns

Node |

Visibility
  internal

## Properties

Property | Action | IE | Mozilla | Netscape | Opera | Safari
--- | --- | --- | --- | --- | --- | ---
attributes : NamedNodeMap Attributes for an element node. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/attributes.html

Remarks

  This property returns a listing of all the attributes bound to this `Node`. Since only `Element` nodes support attributes, most `Node` types will return a null value for this property.

  `NamedNodeMap`s are essentially the DOM-equivilant of a hash. This property provides an alternative to the specific <method>Element.getAttribute</method> and <method>Element.hasAttribute</method> 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 `Element` nodes. Therefore no checking needs to be done ahead of time to verify if a node is an `Element`; rather you can iterate over the <property>attributes</property> 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

childNodes : NodeList Child nodes of the current node. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/children.html

Remarks

  The list of nodes that are immediate children of this node. The DOM is a heirarchial structure of `Node` objects. This property, combined with the <property>parentNode</property> properties provide the basics for defining this structure. Several convenience properties are provided for accessing some frequently-used children nodes, namely the <property>firstChild</property> and <property>lastChild</property> properties.

  Since the `NodeList` class 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 JavaScript `for` loops. Because this property is read-only, you cannot alter the `NodeList` to 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

firstChild : Node First child node of the current node. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/nodetree.html

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.<property>childNodes</property>.<method>NodeList.item</method>(0)`, though this method is much more suscinct and doesn't require any error trapping if the child node doesn't exist.

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

lastChild : Node Last child node of the current node. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/nodetree.html

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.<property>childNodes</property>.<method>NodeList.item</method>(node.<property>childNodes</property>.<property>NodeList.length</property> - 1)`, though this method is much more suscinct and doesn't require any error trapping if the child node doesn't exist.

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

localName : String Local part of an element or attribute name if it the node was defined with an XML Namespace. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

Availability

  HTML DOM Level 2|W3C

namespaceURI : String URI of the namespace for an element or attribute node if the node was defined with an XML Namespace. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

Availability

  HTML DOM Level 2|W3C

nextSibling : Node Sibling node immediately after the current node. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/nodetree.html

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 <property>nextSibling</property> property allows you to traverse to neighboring nodes in the DOM.

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

nodeName : String Name of the node. Same as tag name for element nodes. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/aboutnodes.html

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 `Element` or `Attr` object, then the tag or attribute name is returned. In other cases some place-holder text will be returned.

  <property>nodeName</property> node-type values<columns><column><name>Type</name><align>left</align></column><column><name>Value</name><align>left</align></column></columns><rows><row><column>`Attr`</column><column> Attribute name </column></row><row><column>`CDATASection`</column><column>`#cdata-section`</column></row><row><column>`Comment`</column><column>`#comment`</column></row><row><column>`Document`</column><column>`#document`</column></row><row><column>`DocumentFragment`</column><column>`#document-fragment`</column></row><row><column>`DocumentType`</column><column> Document type name </column></row><row><column>`Element`</column><column> Tag name </column></row><row><column>`Entity`</column><column> Entity name </column></row><row><column>`EntityReference`</column><column> Name of entity referenced </column></row><row><column>`Notation`</column><column> Notation name </column></row><row><column>`ProcessingInstruction`</column><column> Target </column></row><row><column>`Text`</column><column>`#text`</column></row></rows>

  If <glossary>XML Namespaces</glossary> were defined for this node and this node is a `Element` or `Attr` object (since these are the only node types that support the use of <glossary>namespaces</glossary>), this property returns the node name including the <property>prefix</property> and <property>localName</property>.

  Returns an upper case tag name.

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

nodeType : Number Type of node. See Remarks for valid values. | Show Details | 5.5+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

- IE: IE 5.0 on Windows does not assign nodeType attributes. Fixed in IE 5.5.

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/aboutnodes.html

Remarks

  This property returns an integer indicating what type of DOM `Node` this object represents.

  Because the `Node` class 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:

  <property>nodeList</property> constant values<columns><column><name>Value</name><align>right</align></column><column><name>Constant</name><align>left</align></column></columns><rows><row><column>`1`</column><column><property>ELEMENT_NODE</property></column></row><row><column>`2`</column><column><property>ATTRIBUTE_NODE</property></column></row><row><column>`3`</column><column><property>TEXT_NODE</property></column></row><row><column>`4`</column><column><property>CDATA_SECTION_NODE</property></column></row><row><column>`5`</column><column><property>ENTITY_REFERENCE_NODE</property></column></row><row><column>`6`</column><column><property>ENTITY_NODE</property></column></row><row><column>`7`</column><column><property>PROCESSING_INSTRUCTION_NODE</property></column></row><row><column>`8`</column><column><property>COMMENT_NODE</property></column></row><row><column>`9`</column><column><property>DOCUMENT_NODE</property></column></row><row><column>`10`</column><column><property>DOCUMENT_TYPE_NODE</property></column></row><row><column>`11`</column><column><property>DOCUMENT_FRAGMENT_NODE</property></column></row><row><column>`12`</column><column><property>NOTATION_NODE</property></column></row></rows>

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

nodeValue : String Value of the current node. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/aboutnodes.html

Remarks

  This represents the value of the node. Only `Attr`, `CDATASection`, `Comment`, `ProcessingInstruction`, and `Text` objects 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

ownerDocument : Document Document object that contains this node. | Show Details | 6.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/document.html

Remarks

  Refers to the `Document` object that this node exists in. Because a node must be created as a child of a `Document`, and since `Node` objects 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

parentNode : Node Parent node of the current node. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/nodetree.html

Remarks

  This property contains a reference to the parent node for the current node. Since the DOM is a heirarchial structure of nodes, the <property>parentNode</property> and <property>childNodes</property> properties tie the collection of nodes together.

  Not all nodes can have parents however. `Attr`, `Document`, `DocumentFragment`, `Entity` and `Notation` objects 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

prefix : String Namespace prefix for an element or attribute node if the node was defined with an XML Namespace. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

Availability

  HTML DOM Level 2|W3C

previousSibling : Node Sibling node immediately before the current node. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/nodetree.html

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

## Functions

Method | Action | IE | Mozilla | Netscape | Opera | Safari
--- | --- | --- | --- | --- | --- | ---
static appendChild(Node newChild) : Node Appends a node to the childNodes array for the node. | Show Details | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

- IE: Appending an "option" element to a "select" element may crash IE 5.0 on Windows.

Parameters

Node | newChild | Node to add to the current element as a child.

Returns

Node |

  ### 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:

  http://www.quirksmode.org/dom/tests/movingnodes.html

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

  Node.insertBefore|Node.removeChild|Node.replaceChild

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

static cloneNode(Boolean deep) : Node Creates a copy of either the node or the node and its children. | Show Details | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

- IE: On a Mac, IE 5.0 does not clone the event handlers for an element.

Parameters

Boolean | deep | If true, clone the child nodes, also.

Returns

Node |

  ### 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:

  http://www.quirksmode.org/dom/tests/movingnodes.html

Remarks

  Because a `Node` can 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 DOM `Node`. 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 `Node`s are heirarchial, the argument to this method indicates whether or not you would like this node's children cloned as well. If you pass `false` into 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 `Element` node, 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 an `Element` are in fact `Text` nodes, and will not be cloned unless a deep clone is requested.

  If you clone an `Attr` object explicitly, rather than simply cloning an `Element`, the <property>Attr.specified</property> property is set to true, regardless of whether or not the attribute you are cloning from was specified.

  <warning>

  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.

  </warning>
Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

hasAttributes() : Boolean Returns true if the node is an element node with attributes. | Show Details | no | 1.0+ | 6.0+ | 7.0+ | 1.3+

Returns

Boolean |

  ### 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:

  http://www.quirksmode.org/dom/tests/attributes3.html

Remarks
  Returns true if this node is an `Element` and has any attributes. Returns false if there are none. Equivilant to checking `node.`attributes`.<property>NamedNodeMap.length</property> > 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. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

Returns

Boolean |

  ### 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

  Node.childNodes

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

static insertBefore(Node newChild, Node refChild) : Node Inserts a node into the childNodes array for the node before the specified child node. | Show Details | 4.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

Parameters

Node | newChild | Child node to insert.
Node | refChild | Sibling node for the new child.

Returns

Node |

  ### 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:

  http://www.quirksmode.org/dom/tests/movingnodes.html

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 the <varname>refChild</varname> 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 `refChild` node is not a child of this one.
See Also

  Node.appendChild|Node.removeChild|Node.replaceChild

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. | Show Details | no | 1.0+ | 6.0+ | 7.0+ | 1.0+

Parameters

String | feature | Name of the feature.
String | version | (optional)Version of the feature.

Returns

Boolean |

Remarks
  Tests whether a node supports the specified feature. Similar to the `DOMImplementation.hasFeature` method and takes the same feature names. If version is null, returns true if any version of the feature is supported.
See Also

  DOMImplementation.hasFeature

Availability

  HTML DOM Level 2|W3C

normalize() : void Merges text nodes adjacent to the element to create a normalized DOM. | Show Details | N/A | 1.0+ | 6.0+ | 7.0+ | 1.0+

- IE: May crash IE 6.0.

Returns

void |

  For examples, see the quirksmode test page:

  http://www.quirksmode.org/dom/tests/splittext.html

Remarks
  Normalizes any `Text` nodes contained under this node and all of its children. Merges any adjacent `Text` nodes 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 existing `Text` nodes, you can simply add new nodes and perform the `normalize` when you finish your updates.

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

static removeChild(Node oldChild) : Node Removes the specified child node from the element and returns the node. | Show Details | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

Parameters

Node | oldChild | Child node to remove.

Returns

Node |

  ### 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:

  http://www.quirksmode.org/dom/tests/movingnodes.html

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

  Node.appendChild|Node.insertBefore|Node.replaceChild

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

static replaceChild(Node newChild, Node oldChild) : Node Removes the specified child node, replaces it with another node, and returns the removed node. | Show Details | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+

Parameters

Node | newChild | Node to replace the oldChild node with.
Node | oldChild | Node to be replaced.

Returns

Node |

  ### 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:

  http://www.quirksmode.org/dom/tests/movingnodes.html

Remarks
  This method simplifies the process of calling `removeChild` and then `insertBefore`, 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 `DocumentFragment` node, the `oldChild` node is replaced by all of the nodes within the `DocumentFragment`, 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

  Node.appendChild|Node.insertBefore|Node.removeChild

Availability

  HTML DOM Level 1|HTML DOM Level 2|W3C

## 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

Document|Element|Text

## Availability

HTML DOM Level 1|HTML DOM Level 2|W3C

text_javascript aptana_docs