Element getAttributeNode() Method - Javascript DOM

Javascript examples for DOM:Element getAttributeNode

Description

The getAttributeNode() method returns the attribute node by name as an Attr object.

Parameters

Parameter Type Description
attributename String Required. The name of the attribute you want to return

Return Value:

An Attr object, representing attribute node.

The following code shows how to Get the value of the class attribute node of an <h1> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
.democlass {/*from  w ww  .  j a  v a  2 s.  c  om*/
    color: red;
}
</style>
</head>
<body>

<h1 class="democlass">Hello World</h1>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
    var elmnt = document.getElementsByTagName("H1")[0];
    var attr = elmnt.getAttributeNode("class").value;
    document.getElementById("demo").innerHTML = attr;
}
</script>

</body>
</html>

Related Tutorials