Value of the class attribute node of an element in JavaScript?


Javascript has provided the getAttributeNode() method to find the attribute node with the specified name of an element, as an attribute object. If the attribute does not exist, the return value is null or an empty string ("").

syntax

element.getAttributeNode(attributename);

It returns the attribute object representing the specified attribute node 

Example

In the following example, there are two heading tags with different classes. Those tags when accessed by the getAttributeNode() can return the attribute class with which they attached. It works the same as an array. We can access the multiple classes only by providing their index numbers.

Live Demo

<html>
<body>
<h2 class="class1">Tutorix</h2>
<h2 class="class2">Tutorialspoint</h2>
<p id = "attribute"></p>
<script>
   var elmnt = document.getElementsByTagName("h2")[1];
   var value = elmnt.getAttributeNode("class").value;
   document.getElementById("attribute").innerHTML = value;
</script>
</body>
</html>

Output

Tutorix
Tutorialspoint
class2
raja
Published on 08-Aug-2019 14:37:37
Advertisements