Languages
[Edit]
EN

JavaScript - remove class from HTML element on click event

0 points
Created by:
FryerTuck
649

In this article, we would like to show you how to remove class from the HTML element on click event using JavaScript.

Quick solution:

<div onclick="this.classList.remove('class-name')">Click me to remove 'class-name' class.</div>

Where:

  • class-name is the name of the class that will be removed on click event.

 

Practical example

In this example, we create removeClass() function that accepts 2 arguments:

  • element - handle to the HTML element,
  • clazz - class name to remove.

Using the removeClass() function remove background class from the div element.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    .background {
        background: yellow;
    }

    .border {
        border: 1px solid red;
    }

  </style>
</head>
<body>
  <script>

    function removeClass(element, clazz) {
        element.classList.remove(clazz);
    }

  </script>
  <div class="background border" onclick="removeClass(this, 'background')">Click me!</div>
</body>
</html>

See also

  1. JavaScript - add class to HTML element on click event

  2. JavaScript - add, remove, get and check element class name

Alternative titles

  1. JavaScript - remove class from HTML element onclick
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join