David.J David.J - 5 months ago 8
jQuery Question

Correct Way To Remove Class On Second Click

I have multiple divs that on mouse click get a class added.

What is the appropriate way to remove the added class on that particular div on a second click of that item. I have not been able to remove the class.

jQuery('.icon-box').on('click', function() {

var isActive = false;
jQuery(this).addClass('clicked');

if (jQuery(this).hasClass('clicked')) {
var isActive = true;
}
if ( isActive ) {
jQuery(this).removeClass('clicked');
}

});


JSFiddle Example.

Thank you for the feedback.

Answer
jQuery('.icon-box').on('click', function() {
 if (jQuery(this).hasClass('clicked')) {
   $(this).removeClass('clicked');
  }
  else
  {
   $(this).addClass('clicked');
  }

});
Comments