Sunday 30 September 2018

JavaScript: nodeType: get node type

By using nodeType property, you can get the nodeType of any element.

Syntax
node.nodeType

‘nodeType’ property return an integer, each integer mapped to specific element type. Following table summarizes the element type and its corresponding integer.

Integer code
Mapped to element
1
ELEMENT_NODE
2
ATTRIBUTE_NODE
3
TEXT_NODE         
4
CDATA_SECTION_NODE
5
ENTITY_REFERENCE_NODE
6
ENTITY_NODE       
7
PROCESSING_INSTRUCTION_NODE
8
COMMENT_NODE      
9
DOCUMENT_NODE     
10
DOCUMENT_TYPE_NODE
11
DOCUMENT_FRAGMENT_NODE
12
NOTATION_NODE     

childNodes.html
<!DOCTYPE html>

<html>

<head>
    <title>Child Nodes</title>

</head>

<body>
    <p class="color1" id="para1">
        Good Morning
    </p>

    <script>
        var element = document.getElementsByTagName("body")[0];

        var childs = element.childNodes;

        alert("Total child nodes " + childs.length);

        for (var i = 0; i < childs.length; i++) {
            alert(childs[i].textContent + " " + childs[i].nodeType);
        }
    </script>
</body>

</html>


Open above page in browser, you can able to see each childe node and it’s type (Type displays as integer code).






Previous                                                 Next                                                 Home

No comments:

Post a Comment