Object: Node
Availability |
|
Extended by |
Attr, CharacterData, Document, DocumentFragment, DocumentType, Element, Entity, EntityReference, Notation, ProcessingInstruction |
The Node object represents a unique node within the DOM tree. It is the primary data type for the DOM.
While Node exposes methods for dealing with children, not all objects inheriting from Node may have children. For example, Text nodes do not permit children; trying to add children to such nodes results in a DOMException being raised.
The Node.nodeName, Node.nodeValue, and Node.nodeType properties enable you to acquire node information without casting down to the specific child object. In cases where there is no obvious mapping of these properties for a specific nodeType (for example, the Node.nodeValue for an Element, or attributes for a Comment), they return null.
The specialized objects may contain additional and more convenient mechanisms to get and set the relevant information.
Node properties
Node methods
Method name |
Description |
Availability |
|---|---|---|
Adds an EventListener object to a set of event listeners for the given node. |
4.6 or later |
|
Adds a node to the end of the array of child nodes for the given node. |
4.6 or later |
|
Returns a duplicate of the given node. The duplicate node has no parent. |
4.6 or later |
|
Dispatches an Event object created by Document.createEvent(). |
4.6 or later |
|
Returns whether the given node has any attributes. |
4.6 or later |
|
Returns whether the given node has any children. |
4.6 or later |
|
Inserts a new node before the given node. |
4.6 or later |
|
Tests whether the given node supports a specific feature. |
4.6 or later |
|
Merges text nodes adjacent to the given element node to create a normalized DOM. |
4.6 or later |
|
Removes the specified child from the given element node and returns it. |
4.6 or later |
|
Removes an event listener from an EventTarget. |
4.6 or later |
|
Removes the specified child from the given and replaces it with another node, then returns the removed node. |
4.6 or later |
Method: Node.removeEventListener()
The removeEventListener() method removes an EventListener from an EventTarget.
Parameters
Parameter |
Type |
Description |
|---|---|---|
type |
String |
The type of event. |
listener |
The EventListener function to be removed. |
|
useCapture |
boolean |
When true, indicates that the EventListener being removed was registered as a capturing listener. A listener may be registered twice, once as a capturing listener, and once as a non-capturing listener. Each must be removed separately. |
Method: Node.replaceChild()
The replaceChild() method removes the specified child from the current node and replaces it with another node, then returns the replaced node.
Parameters
Parameter |
Type |
Description |
|---|---|---|
newChild |
The node with which to replace the old node with. If newChild is a DocumentFragment object, then the entire contents of the document fragment are appended to the given node. If newChild already exists as a child of the current node in the tree, it is removed, and then replaced. |
|
oldChild |
The child node to replace. |
Exceptions
Exception |
Description |
|---|---|
HIERARCHY_REQUEST_ERR |
This error is thrown if either newChild or oldChild is one of the current node’s ancestors, or if the current node does not allow children of the type of newChild node. |
NO_MODIFICATION_ALLOWED_ERR |
This error is thrown if the current node is read only. |
NOT_FOUND_ERR |
This error is thrown if oldChild is not a child of the current node. |
WRONG_DOCUMENT_ERR |
This error is thrown if newChild was created from a different document than the current node. |
Method: Node.normalize()
Method: Node.addEventListener()
The addEventListener() method adds an EventListener object to a set of event listeners for the given node.
Parameters
Parameter |
Type |
Description |
|---|---|---|
type |
String |
The type of event to add. |
listener |
The event listener function to be invoked. |
|
useCapture |
boolean |
When true, indicates all events of the specified type to the registered EventListener before being dispatched to any EventTargets beneath the given node in the tree. Bubbling events will not trigger the EventListener. When false, this method dispatches events of the specified type to the registered EventListener before being dispatched to any EventTargets above the given node in the tree. |
Method: Node.appendChild()
Parameters
Parameter |
Type |
Description |
|---|---|---|
newChild |
The node to add. If newChild is a DocumentFragment object, then the entire contents of the document fragment are appended to the given node. If newChild already exists in the tree, it is removed, and then replaced. |
Exceptions
Exception |
Description |
|---|---|
HIERARCHY_REQUEST_ERR |
This error is thrown if newChild is one of the given node’s ancestors, or if the given node does not allow children of the type of newChild node. |
NO_MODIFICATION_ALLOWED_ERR |
This error is thrown if the given node is read only. |
WRONG_DOCUMENT_ERR |
This error is thrown if newChild was created from a different document than the given node. |
Method: Node.cloneNode()
The cloneNode() method returns a duplicate of the given node. The duplicate node has no parent.
If you clone an Element or an Attr node, you should be aware of the following:
When you clone an Element node, you also duplicate its attributes and their values. However, you do not clone any text the Element node contains (since the text is contained in a child text node) unless you specify the deep parameter.
When you clone an Attr node, this method returns a "specified" attribute (that is, the specified property for that attribute is set to true).
Cloning any other type of node simply returns a copy of the node.
Method: Node.dispatchEvent()
The dispatchEvent() method dispatches an Event object created by Document.createEvent().
Return values
Returns true if the Event.preventDefault() was not called by any of the event listeners that handled the event. The Event.preventDefault() method prevents the default action for the event from occurring; otherwise, returns false.
Method: Node.hasAttributes()
Method: Node.hasChildNodes()
Method: Node.insertBefore()
The insertBefore() method inserts a new child node into the current node before the specified child node. This methods allows you to insert a node at a specific location among a number of child nodes.
Parameters
Parameter |
Type |
Description |
|---|---|---|
newChild |
The node to insert. If newChild is a DocumentFragment object, then the entire contents of the document fragment are inserted, in order, before refChild. If newChild already exists as a child of the current node in the tree, then it is removed, and the new node inserted. |
|
refChild |
The existing child node before which newChild is inserted. If refChild is not specified or is null, then newChild is inserted at the end of the list of children. |
Exceptions
Exception |
Description |
|---|---|
HIERARCHY_REQUEST_ERR |
This error is thrown if newChild is an ancestor of refChild, or if refChild does not allow children of the type of newChild node. |
NO_MODIFICATION_ALLOWED_ERR |
This error is thrown if the given node is read only. |
NOT_FOUND_ERR |
This error is thrown if refChild is not a child of the current node. |
WRONG_DOCUMENT_ERR |
This error is thrown if newChild was created from a different document than the current node. |