Languages
[Edit]
EN

JavaScript - remove class from div

7 points
Created by:
Root-ssh
69140

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>

 

Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join