In this section, we will learn what the Element nodeType property is and how to use it in JavaScript.
What is Element nodeType Property in JavaScript?
The JavaScript Element nodeType property is used to get the type of the element that invoked this property.
For example, using this property, we can see if an element is an Element-Node, Text-Node, or a Comment-Node.
JavaScript nodeType Property Syntax:
element.nodeType;
JavaScript nodeType Property Input Value
The nodeType property is read-only and so we can’t assign a value to it.
JavaScript nodeType Property Return Value
If the target element is an element node, the nodeType property will return 1.
If the target element is an attribute node, the nodeType property will return 2.
If the target element is a text node, the nodeType property will return 3.
If the target element is a comment node, the nodeType property will return 8.
Example: using nodeType Property in JavaScript
See the Pen using nodeType Property in JavaScript by Omid Dehghan (@odchan1) on CodePen.
The value 1 returned as a result of calling the nodeType property on the <ol> element. This means <ol> is an Element-Node.
Note: use the nodeName property if you want to know the name of the target element.