How to add, remove and toggleclass in jQuery

author details
AdiPie
4th Jan 2021
1 min read
Image
add, remove and  toggleclass in jQuery

JQuery has some methods for controlling CSS class and its styles. Here, we will discuss some of the strategies which are used in jQuery:

addClass() - Adds atleast one or more classes to the selected tags or element. 

removeClass() - Removes atleast one or more classes to the selected tags or element. 

toggleClass() - Toggles between adding/eliminating classes from the chose components


Example:

.custom_prop{
  font-weight: bold;
  font-size: 2rem;
}

$("button").click(function(){
  $("body").addClass("custom_prop");

  $("body").removeClass("custom_prop");

  $("h2").toggleClass("custom_prop");
});