Edit This Code:
See Result »
<!DOCTYPE html> <html> <head> <style> .mystyle { width: 300px; height: 100px; background-color: coral; color: white; margin-bottom: 10px; } </style> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "//hm.baidu.com/hm.js?73c27e26f610eb3c9f3feb0c75b03925"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </head> <body> <p>Click the button to display the value of the class attribute of the div element below.</p> <div id="myDIV" class="mystyle"> I am a DIV element with class="mystyle" </div> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var x = document.getElementsByClassName("mystyle")[0].className; var y = document.getElementById("myDIV").className; document.getElementById("demo").innerHTML = "Using getElementsByClassName: " + x + "<br>" + "Using getElementById: " + y; } </script> </body> </html>
Result: