Javascript examples for DOM:Element getAttribute
The getAttribute() method returns the attribute value by name.
Parameter | Type | Description |
---|---|---|
attributename | String | Required. The name of the attribute you want to get the value from |
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:
<!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>