You are close to doing it right, the mechanism you created by checking to see if your element has an active class, but jQuery has toggleClass() , which allows you to simply write the following:
$('.btn').click(function() { $(this).toggleClass('active'); });
Then, in your CSS, instead of the psuedo :active style, you will use the class name instead:
.btn.active { background: #cecece; text-decoration: none; }
You will also want to remove the :active selector from your CSS alltogether, since you won't need it anymore :)
As dfsq , it is indicated that there is a certain value when saving the selector :active psuedo:
I just think that the active state (: active) for the controls is important. For example, without it, the button will look the same if it is pressed, but not pressed (mouse to mouse). UX is slightly better with: active. But maybe I'm too picky
For this reason, you can change the selector as:
.btn.active, .btn:active { background: #cecece; text-decoration: none; }
Since this will affect the state of .active and :active .
source share