Element classList Property - Find out if an element has a "mystyle" class: - Javascript DOM

Javascript examples for DOM:Element classList

Description

Element classList Property - Find out if an element has a "mystyle" class:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
.mystyle {/* w  w w. jav a  2  s.  c o  m*/
    width: 500px;
    height: 50px;
    border: 1px solid black;
}

.anotherClass {
    background-color: lightblue;
    padding: 25px;
}

.thirdClass {
    text-align: center;
    font-size: 25px;
    color: navy;
    margin-bottom: 10px;
}
</style>
</head>
<body>

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

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

<script>
function myFunction() {
    var x = document.getElementById("myDIV");

    if (x.classList.contains("mystyle")) {
        x.classList.remove("anotherClass");
    } else {
        console.log("Could not find it.");
    }
}
</script>

</body>
</html>

Related Tutorials