jQuery Remove Class From HTML Elements Example

Today, We want to share with you jquery remove class.In this post we will show you jquery add removes class onclick, hear for jquery add all class we will give you demo and example for implement.In this post, we will learn about jQuery removeClass Method with an example.

How to use removeClass() method in jQuery?

Example 1: index.html

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").removeClass("details");
  });
});
</script>
<style>
.details {
  font-size: 100%;
  color: green;
}
</style>
</head>
<body>

<h2>Welcome To Pakainfo.com</h2>

<p class="details">Pakainfo - Web Development & Good Online education</p>
<p class="details">This is Web Development & Good Online education Tutorials.</p>

<button>Remove the "details" class from all p elements</button>

</body>
</html>


addClass and removeClass in jQuery

HTML Code

<div class="clickable spot" id="spot1">
    <div class="white"></div>
    <article class="purple" id="story1">
        <div>
            <p class="date">posted 20 Jan 14</p>
            <p class="region">Bangladesh</p>
            <p class="title">Story Title</p>
            <p>Egestas eros ac risus faucibusr</p>
            <a class="close_button">CLOSE</a>
        </div>
    </article>
</div>

jQuery Code

$(document).ready(function() {   
    $(".close_button").click(function (e) { 
        $("#spot1").addClass("spot");
        $("#spot1").removeClass("grown");
        e.stopPropagation();
    });  

    $(".clickable").click(function () {
        if ($(this).hasClass("spot")){
            $(this).addClass("grown");
            $(this).removeClass("spot");
        }
    });
});

CSS Code

.spot {
    background: #AAA;
}

.grown {
    background: #0A0;
}

.close_button {
    display: none;
}

.grown .close_button {
    display: inline;
}

Example 3: Usage of .removeClass()

$("button").click(() => {
    $("div").removeClass("introduction");
});

Example 4: No argument

$().removeClass()

Example 5: Space separated list as an argument

$().removeClass([className])

Removing all classes

index.html

<html>
 <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

   <script>
       $(document).ready(function(){  
     $("#bttn").click(function(){
       $("h1").removeClass();
     });
       });
   </script>
 </head>
 <body>
   <h1 class="offer_code high_attect italics">Educative!</h1>
   <button id="bttn">Remove Class!</button>
 </body>
</html>

Space separated list as an argument

<html>
 <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

   <script>
       $(document).ready(function(){  
     $("#bttn").click(function(){
       $("h1").removeClass("offer_code high_attect");
     });
       });
   </script>
 </head>
 <body>
   <h1 class="offer_code high_attect italics">Educative!</h1>
   <button id="bttn">Remove Class!</button>
 </body>
</html>

Function as an argument

<html>
 <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

   <script>
       $(document).ready(function(){  
     $("#bttn").click(function(){
       $("h1").removeClass(function(){
           return $(this).attr( "class" );
       });
     });
       });
   </script>
 </head>
 <body>
   <h1 class="offer_code high_attect italics">Educative!</h1>
   <button id="bttn">Remove Class!</button>
 </body>
</html>

I hope you get an idea about jQuery Get and Set CSS Classes.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  jquery set select option value

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

Your email address will not be published. Required fields are marked *