Languages
[Edit]
EN

JavaScript - remove class from div

7 points
Created by:
Root-ssh
6445

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>

 

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.