Skip to content Skip to sidebar Skip to footer

Jquery - Add Css Class To Button Element After Click

I want to change the background color of a button permanently after it is clicked. Using the following code the color only changes while the button is active/being clicked. Once th

Solution 1:

The only problem is that you are missing a curly brace: http://jsfiddle.net/AaKuf/

 $(document).ready(function(){
      $('button').click(function(){
           $(this).addClass('clicked');
      });
 }); //<==here

Solution 2:

$('button.inactive').click(function() {
    $(this).removeClass('inactive').addClass('clicked');
});

Should do the trick. This will clear the old class away from the button and give it the proper .clicked class. If the colour is resetting after using this solution, then the problem is elsewhere in your code.

Solution 3:

$('button').mouseup(function() { 
    $(this).removeClass('clicked').addClass('inactive');
}

Post a Comment for "Jquery - Add Css Class To Button Element After Click"