Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html> <head> <style> .Class1 { width: 500px; height: 30px; } .Class2 { background-color: lightblue; } .Class3 { text-align: center; font-size: 25px; color: black; margin-bottom: 10px; padding: 10px; } </style> </head> <body> <p>Click the button to display the class name of the first class (index 0) of div.</p> <div id="myDiv" class="Class1 Class2 Class3"> I am a Div element with three classes. </div> <button onclick="myFunction()">Click</button> <p><strong>Note:</strong> The classList property is not supported in Internet Explorer 9 and earlier versions.</p> <p id="demo"></p> <script> function myFunction() { var x = document.getElementById("myDiv").classList.item(0); document.getElementById("demo").innerHTML = x; } </script> </body> </html>