The Node
class is accessible as DOM.Node
, not because it can be directly instantiated (trying will throw an error), but rather to give access to the static node type constants that are available on Node
.
Unlike the standard DOM API, there is no separate Element
class for elements. Instead, Node
includes the necessary properties and methods to be able to take the place of the standard Element
class, reducing complexity and code size.
- Properties
- Methods
- Node Type Constants
-
attributes
Object — [standard] [MDN]For
ELEMENT_NODE
type nodes, thisObject
is a key/value pair of (mostly) strings that represent the names and values of each of the element's attributes. If an attribute was specified without a value, the value for the attribute in thisObject
will be the boolean valuetrue
. Unlike the standard, thisObject
is only indexable by attribute name, not by numeric index.For all other node types, the
attributes
property will not exist. -
childNodes
Array — [standard] [MDN]An
Array
containing all of the children (of all node types) of this node. This property only exists on nodes of typeELEMENT_NODE
,DOCUMENT_NODE
, andDOCUMENT_FRAGMENT_NODE
. ForDOCUMENT_NODE
type nodes, thisArray
should only ever include two nodes, the<!DOCTYPE>
declaration and the document's root element.
-
classList
DOMTokenList or Null (read-only) — [standard] [MDN]Gets a
DOMTokenList
instance, forELEMENT_NODE
type nodes, containing the case-sensitive list of CSS class names assigned to thisNode
. -
className
String — [standard] [MDN]The space separated list of CSS class names assigned to this
Node
. -
firstChild
Node or Null (read-only) — [standard] [MDN]The first child node of this node, or
null
if this node has no children. -
Gets this node's
id
attribute, or an empty string if it either doesn't have one or this node isn't of typeELEMENT_NODE
. -
innerHTML
String — [standard] [MDN]For
ELEMENT_NODE
type nodes, this gets or sets the HTML markup inside this node. -
lastChild
Node or Null (read-only) — [standard] [MDN]The last child node of this node, or
null
if this node has no children. -
nextSibling
Node or Null (read-only) — [standard] [MDN]The next
Node
, in document order, that is a sibling to this node (ie. both nodes have the same parent node), ornull
if this node is the last child of its parent. -
nodeName
String (read-only) — [standard] [MDN]Gets the name of this node, which is dependent on this node's type:
Node Type Value CDATA_SECTION_NODE "#cdata-section"
COMMENT_NODE "#comment"
DOCUMENT_FRAGMENT_NODE "#document-fragment"
DOCUMENT_NODE "#document"
DOCUMENT_TYPE_NODE The value of this document type's name
propertyELEMENT_NODE The value of this element's tagName
propertyPROCESSING_INSTRUCTION_NODE The value of this processing instruction's target
propertyTEXT_NODE "#text"
-
nodeType
Node Type [Number] (read-only) — [standard] [MDN]One of the node type constants that represent the type of the node.
-
nodeValue
String or Null — [standard] [MDN]This is the text content, as a
String
, for node types that are text-based (text nodes, comments, CDATA sections, and processing instructions). For all other node types, this property isnull
.⚠️ Caution: For text-based nodes, setting this property to something other than aString
could result in unexpected errors. -
outerHTML
String — [standard] [MDN]For
ELEMENT_NODE
type nodes, this gets or sets the HTML markup that represents this node. SettingouterHTML
will entirely replace this node with the result of parsing the specified string, or simply remove this node if the specified string isn't valid markup. -
ownerDocument
DOM or Null (read-only) — [standard] [MDN]The top-level
DOM
object that contains this node, ornull
if this node is aDOM
object. -
parentNode
Node or Null (read-only) — [standard] [MDN]Gets the parent node of this node, or
null
if this node isn't attached as a child to any other node (ie. it was just created, or was removed). -
previousSibling
Node or Null (read-only) — [standard] [MDN]The previous
Node
, in document order, that is a sibling to this node (ie. both nodes have the same parent node), ornull
if this node is the first child of its parent. -
tagName
String or Null (read-only) — [standard] [MDN]For
ELEMENT_NODE
type nodes, this gets the tag name of this node in upper case. -
textContent
String — [standard] [MDN]Gets the text content of this node and all of its child nodes, in document order, as a single
String
(excluding CDATA sections, comments, and processing instructions). SettingtextContent
will replace any child nodes of this node with the specified string as a newNode
of typeTEXT_NODE
, or set thenodeValue
to the specified string for text-based nodes.
node.forEach( callback[, type] )
Executes the specified callback
function for each Node
that is a descendent of this node, in document order. If the callback
function returns the Boolean
value false
, forEach()
will return and no further nodes will be visited.
Parameters
-
callback
FunctionThe function to execute on each
Node
, with the following arguments:-
current
NodeThe current
Node
being processed. -
parent
NodeThe
parentNode
of the currentNode
.
-
-
type
Node Type [Number] (optional, default:ELEMENT_NODE
)A node type constant indicating which type of nodes to call the
callback
on, ornull
if you want to visit all nodes regardless of node type. Only one node type may be specified.
node.getRootNode()
Return Value
The top most level Node
that this node is a child of. This can be a DOM
instance if this node is attached to a document, or it can be the same Node
instance getRootNode()
was called on if the node isn't attached to a document and isn't a child of another node.
node.cloneNode( [deep] )
Returns a duplicate of this node.
Parameters
-
deep
Boolean (optional)If specified as
true
, indicates whether all of this node's child hierarchy should also be cloned.
Return Value
The Node
that was cloned from this node.
Only ELEMENT_NODE
type nodes can have attributes, so the below methods that work on attributes will treat all other node types as though they simply have no attributes (since they don't).
node.getAttribute( name )
Returns the value of the specified attribute.
Parameters
-
name
StringThe name of the attribute whose value you want to get. This string will be lower-cased if you specify the
lowerAttributeCase
option astrue
on theDOM
instance this node belongs to.
Return Value
The value of the specified attribute, or null
if the attribute doesn't exist. A non-null
value will normally be a String
, however it can also be the Boolean
value true
if the attribute exists, but doesn't have an explicit value (eg. the element <button disabled></button>
has an attribute named "disabled" that doesn't have a explicit value).
node.getAttributeNames()
Returns an Array
of the names of the attributes on this node, in the order they were created on the node.
Return Value
An Array
containing the names of this node's attributes, as String
values. The array will be empty if the node has no attributes.
node.hasAttribute( name )
Returns whether this node has an attribute whose name matches the specified name.
Parameters
-
name
StringThe name of the attribute whose existence you want to query. This string will be lower-cased if you specify the
lowerAttributeCase
option astrue
on theDOM
instance this node belongs to.
Return Value
A Boolean
indicating whether this node has the specified attribute.
node.hasAttributes()
Returns whether this node has any attributes.
Return Value
A Boolean
indicating whether this node has any attributes.
node.removeAttribute( name )
Removes the specified attribute from this node. If the specified attribute doesn't exist, this method does nothing.
Parameters
-
name
StringThe name of the attribute you want to remove. This string will be lower-cased if you specify the
lowerAttributeCase
option astrue
on theDOM
instance this node belongs to.
node.setAttribute( name, value )
Sets the value of the specified attribute to the specified value, updating a pre-existing attribute with the same name or creating a new attribute.
Parameters
-
name
StringThe name of the attribute you want to set. This string will be lower-cased if you specify the
lowerAttributeCase
option astrue
on theDOM
instance this node belongs to. -
value
String or BooleanThe value to assign to the specified attribute. When
value
is aString
or theBoolean
valuetrue
, the attribute's value is set directly tovalue
. In all other cases,value
is coerced to aString
and the attribute's value is set to the result of the coercion.A
Boolean
value oftrue
is used to represent an attribute that exists, but has no explicit value. This is mostly useful when the node is output as text.
node.toggleAttribute( name[, force] )
Toggles the existence of the specified attribute (ie. the attribute is removed if it exists, otherwise it's added and set to the Boolean
value true
).
Parameters
-
name
StringThe name of the attribute you want to toggle. This string will be lower-cased if you specify the
lowerAttributeCase
option astrue
on theDOM
instance this node belongs to. -
force
Boolean (optional)If specified, indicates whether to only add the specified attribute (when
force
istrue
), or only remove the specified attribute (whenforce
isfalse
).
Return Value
A Boolean
indicating whether the specified attribute exists after the call to toggleAtribute()
.
The below methods that work on child nodes will only work on nodes that can have child nodes (nodes of type ELEMENT_NODE
, DOCUMENT_NODE
, and DOCUMENT_FRAGMENT_NODE
). For any method that inserts a node in some fashion, if the inserted node is a document fragment (a DOCUMENT_FRAGMENT_NODE
type node), all of the top-level children of the document fragment will be removed from the fragment and then inserted in place of the document fragment.
node.appendChild( child )
Adds the specified node child
to the end of this node's childNodes
array. If the specified node is already in a document hierarchy, it is first removed and then added to this node's children.
Parameters
-
child
NodeThe node to append to the end of this node's
childNodes
array.
Return Value
The Node
that was appended, which is usually the node specified as child
. If the specified node is a document fragment, the empty document fragment node will be returned.
node.hasChildNodes()
Returns whether this node has any child nodes.
Return Value
A Boolean
indicating whether this node has any child nodes.
node.insertBefore( newChild, refChild )
Inserts the node newChild
into this node's childNodes
array before the node refChild
.
Parameters
-
newChild
NodeThe node to be inserted.
-
refChild
Node or NullThe node before which the node
newChild
should be inserted. IfrefChild
isnull
,insertBefore()
will act exactly likeappendChild()
. Otherwise, ifrefChild
isn't aNode
, or isn't a direct child of this node (the nodeinsertBefore()
was called on), the nodenewChild
will not be inserted.
Return Value
The Node
that was inserted, which is usually the node specified as newChild
, or null
if no node was inserted. If newChild
is a document fragment, the empty document fragment node will be returned.
node.removeChild( child )
Removes the specified node from this node's childNodes
array.
Parameters
-
child
NodeThe node to be removed. If the node isn't a direct child of this node (the node
removeChild()
was called on), the nodechild
will not be removed.
Return Value
The Node
that was removed, or null
if no node was removed.
node.replaceChild( newChild, oldChild )
Replaces the node oldChild
with the node newChild
.
Parameters
-
newChild
NodeThe node to replace the node
oldChild
with. -
oldChild
NodeThe node being replaced. If the node isn't a direct child of this node (the node
replaceChild()
was called on), the nodeoldChild
will not be replaced.
Return Value
The Node
that was replaced, or null
if no node was replaced.
node.closest( selector )
Starting with this Node
, closest()
traverses the node hierarchy toward the root node looking for the first ELEMENT_NODE
type node that matches the specified selector.
Parameters
-
selector
StringThe selector string to match against. See the selectors documentation for a list supported selectors.
Return Value
The Node
that was first matched by the specified selector, or null
if no node matched.
Exceptions
A SyntaxError
is thrown if the specified selector is invalid. The message of the SyntaxError
will have a more detailed description of what exactly the error is, and the stack
property of the error object will have information about where in the selector the error was encountered.
node.getElementById( id )
Gets the first ELEMENT_NODE
type node which has an id
attribute that matches the specified id
parameter string.
Parameters
-
id
StringThe
id
attribute of the element to locate. The comparison is case sensitive.
Return Value
The Node
whose id
attribute matches the specified ID, or null
if no element was found.
node.getElementsByClassName( className )
Gets all ELEMENT_NODE
type nodes which have a class
attribute that contains the specified className
parameter string and are descendents of this node.
Parameters
-
className
StringOne or more class names to locate, separated by whitespace. All class names must exist on an element for it to be considered matching. The comparison is case sensitive.
Return Value
An Array
of all elements (that are children of the element this method was called on) whose class
attribute contains the specified class name or names. The array will be empty if no elements matched.
node.getElementsByTagName( tagName )
Gets all ELEMENT_NODE
type nodes which have a tagName
that matches the specified tagName
parameter string and are descendents of this node.
Parameters
-
tagName
StringThe
tagName
of the elements to locate. The comparison is not case sensitive.
Return Value
An Array
of all elements (that are children of the element this method was called on) whose tagName
matches the specified one. The array will be empty if no elements matched.
node.matches( selector )
Return whether this node would be selected by the specified selector.
Parameters
-
selector
StringThe selector string to match against. See the selectors documentation for a list supported selectors.
Return Value
A Boolean
indicating whether this node would be selected by the specified selector.
Exceptions
A SyntaxError
is thrown if the specified selector is invalid. The message of the SyntaxError
will have a more detailed description of what exactly the error is, and the stack
property of the error object will have information about where in the selector the error was encountered.
node.querySelector( selector )
Returns the first ELEMENT_NODE
type node that matches the specified selector and is a descendent of this node.
Parameters
-
selector
StringThe selector string to match against. See the selectors documentation for a list supported selectors.
Return Value
The Node
that was first matched by the specified selector, or null
if no node matched.
Exceptions
A SyntaxError
is thrown if the specified selector is invalid. The message of the SyntaxError
will have a more detailed description of what exactly the error is, and the stack
property of the error object will have information about where in the selector the error was encountered.
node.querySelectorAll( selector )
Gets all ELEMENT_NODE
type nodes that match the specified selector and are descendents of this node.
Parameters
-
selector
StringThe selector string to match against. See the selectors documentation for a list supported selectors.
Return Value
An Array
of all elements that match the specified selector. The array will be empty if no elements matched.
Exceptions
A SyntaxError
is thrown if the specified selector is invalid. The message of the SyntaxError
will have a more detailed description of what exactly the error is, and the stack
property of the error object will have information about where in the selector the error was encountered.
Constant | Value | Description |
---|---|---|
Node.ELEMENT_NODE |
1 | An element node (such as <p> or <div> ) that has a tagName property, along with supporting attributes and child nodes. |
Node.TEXT_NODE |
3 | A text node that only stores plain text as a String in its nodeValue property. |
Node.CDATA_SECTION_NODE |
4 | A CDATA section node, commonly used in XML documents to include portions of text without the need to escape the characters < and & . |
Node.PROCESSING_INSTRUCTION_NODE |
7 | A processing instruction node, commonly used in XML documents to embed application-specific instructions. These nodes have a target property rather than the tagName property of element nodes, and do not support attributes or child nodes. Unlike browsers, all other text inside the node (other than the target) is stored in the node's nodeValue property as a String . |
Node.COMMENT_NODE |
8 | A comment node (such as <!-- comment --> ) that stores the text content of the comment in the node's nodeValue property as a String . |
Node.DOCUMENT_NODE |
9 | A document node, created by instantiating the DOM class. |
Node.DOCUMENT_TYPE_NODE |
10 | A document type node. |
Node.DOCUMENT_FRAGMENT_NODE |
11 | A document fragment node, created by instantiating the DOM class without either a <!DOCTYPE> or an <html> element. |