how to add and remove class in jquery on click code example

Example 1: jquery remove and add class

$( "#foo" ).toggleClass( 'className', addOrRemoveOptional );

//OR
if ($( "#foo" ).hasClass('className')) {
	$( "#foo" ).removeClass( 'className');
} else {
  $( "#foo" ).addClass( 'className');
}

Example 2: jquery add remove class clicked element

$(function() {
    $('img').click(function() {
       $(this).toggleClass('active');
    });
});

Example 3: add and remove class in jquery

$('#toggle li').on('click', function () {
    $(this).toggleClass('open')
});

Tags:

Css Example