Element className Property - Get the class name of the first <div> element in the document - Javascript DOM

Javascript examples for DOM:Element className

Description

Element className Property - Get the class name of the first <div> element in the document

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
.mystyle {//from   ww w  .  j  a  v  a  2  s  .co  m
    width: 300px;
    height: 100px;
    background-color: coral;
    color: white;
    margin-bottom: 10px;
}
</style>
</head>
<body>

<div class="mystyle">
I am a DIV element with class="mystyle"
</div>

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

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

<script>
function myFunction() {
    var x = document.getElementsByTagName("DIV")[0].className;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials