Languages
[Edit]
EN

jQuery - remove class from div

6 points
Created by:
Kate_C
2895

In this article we can see how to dynamically remove one class from div element using jQuery.

Quick solution:

$("#my-element").removeClass("my-class-2");

Full code example:

// ONLINE-RUNNER:browser;


<!doctype html>
<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
 <style>
   .my-class-1 { border: 2px solid red; padding: 5px; width: 200px; }
   .my-class-2 { background: yellow; }
 </style>
 <script>
   
   $(document).ready(function() {
     $("#my-button").click(function() {
       // HERE:
       $("#my-element").removeClass("my-class-2");
     });
   });
   
   </script>
</head>
<body>
 <div id="my-element" class="my-class-1 my-class-2">Some text</div>
 <button id="my-button" style="margin-top: 20px;">Remove class</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.