Element getAttribute() Method - Javascript DOM

Javascript examples for DOM:Element getAttribute

Description

The getAttribute() method returns the attribute value by name.

Parameter Values

Parameter Type Description
attributename String Required. The name of the attribute you want to get the value from

Return Value:

A String, representing the specified attribute's value.

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

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
.democlass {/*w w  w  .  j  a  va  2s.  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 x = document.getElementsByTagName("H1")[0].getAttribute("class");
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials