Element classList Property - Toggle between two classes for a <div> element: - Javascript DOM

Javascript examples for DOM:Element classList

Description

Element classList Property - Toggle between two classes for a <div> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
.mystyle {/* www  .ja  va2  s  .  co  m*/
    width: 300px;
    height: 50px;
    background-color: coral;
    color: white;
    font-size: 25px;
}

.newClassName {
    width: 400px;
    height: 100px;
    background-color: lightblue;
    text-align: center;
    font-size: 25px;
    color: navy;
    margin-bottom: 10px;
}
</style>
</head>
<body>

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

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

<script>
function myFunction() {
    document.getElementById("myDIV").classList.toggle("newClassName");
}
</script>

</body>
</html>

Related Tutorials