JavaScript - getAttributeNode() method

revision:


Category : element

The getAttributeNode() method returns the specified attribute of the specified element, as an Attr node.

The getAttributeNode() method returns an element's attribute. Itreturns an Attribute object.

The getAttribute() method returns the value of an attribute., while the getAttributeNode() method returns an Attr object, and you must use the Attr value property to get the value. The result will be the same.

Syntax :

        getAttributeNode(attrName)
    

Parameters:

attrName : required; is a string containing the name of the attribute.

Examples:

            // html: <div id="top" />
            let t = document.getElementById("top");
            let idAttr = t.getAttributeNode("id");
            alert(idAttr.value === "top");
        

Practical examples

example: use getAttributeNode() method on <a> element.
my website

target value :

code:
                    <div>
                        <a href="https://lwitters.com" target="_blank">my website</a>
                        <button onclick="getFunction()">click me</button>
                        <p>target value : <span id="get-1"></span></p>
                    </div>
                    <script>
                        function getFunction(){
                            var a=document.getElementsByTagName("a")[0];
                            var x=document.getElementById("get-1");
                            x.innerHTML=a.getAttributeNode("target").value;
                        }
                        
                    </script>
                

example: get the value of the "class" attribute node of a <h4> element.

The Element Object

The value of the class attribute node of the first h4 element is:

code:
                    <div>
                        <h4 class="democlass">The Element Object</h4>
                        <p>The value of the class attribute node of the first h4 element is:</p>
                        <p id="get-2"></p>
                    </div>
                    <script>
                        const element = document.getElementsByTagName("h4")[0];
                        const attr = element.getAttributeNode("class");
                        document.getElementById("get-2").innerHTML = attr.value;            
                    </script>
                

example: get the value of the "onclick" attribute node of a <button> element.

Click the button to display the value of the onclick attribute node of the button element.

code:
                    <div>
                        <p>Click the button to display the value of the onclick attribute node of the button element.</p>
                        <button id="myBtn" onclick="getFunction2()">Try it</button>
                        <p id="get-4"></p>
                    </div>
                    <script>
                        function getFunction2() {
                            var elmnt = document.getElementById("myBtn");
                            var attr = elmnt.getAttributeNode("onclick").value;
                            document.getElementById("get-4").innerHTML = attr;
                        }
                    </script>