EN
JavaScript - remove class from div
7
points
In this short tutorial we're going to see how to remove class from div element using JavaScript.
Quick solution:
function removeClass() {
var element = document.getElementById("my-element");
element.classList.remove("my-class-2");
}
Full runnable code example:
// ONLINE-RUNNER:browser;
<!doctype html>
<html lang="en">
<head>
<style>
.my-class-1 {
border: 3px solid red;
padding: 5px;
width: 200px;
}
.my-class-2 {
background: yellow;
}
</style>
<script>
function removeClass() {
var element = document.getElementById("my-element");
element.classList.remove("my-class-2");
}
</script>
</head>
<body>
<div class="my-class-1 my-class-2" id="my-element">
Some text
</div>
<button onclick="removeClass()" style="margin-top: 20px;">
Click this button to remove class from element
</button>
</body>
</html>