Perhaps you could consider streamlining the code to something like this:
$('a div[class^="button"]').click(
function(e){
e.stopPropagation();
$('.button_active')
.removeClass('button_active')
.addClass('button_normal');
$(this).addClass('button_active').removeClass('button_normal');
});
Here's a JS Fiddle demo.
Update based on the question from the original poster:
I noticed that after implementing [the updated code in the Fiddle], it uses "button_hover" instead of "button_active". Any idea why?
That behavior is due to how CSS specificity works - I manage classes dynamically to respond to events, avoiding repetitive checks for specific class states. This results in having both classes present like
class="button_normal button_hover"
, with priority given to the later-declared class which maintains "button_hover". It may seem complex, but essentially achieves the desired functionality.
The below code incorporates all necessary modifications and updates to meet your requirements:
$('a div[class^="button"]').hover(
function(){
$(this).addClass('button_hover').click(
function(e){
e.preventDefault();
$('.button_active')
.addClass('button_normal')
.removeClass('button_active');
$(this).addClass('button_active').removeClass('button_hover');
});
},
function(){
$(this).removeClass('button_hover');
});
CSS:
.button_active,
.button_normal.button_active { background: #000; }
.button_normal.button_hover { background: #ff0; }
.button_normal { background: #d89; }
View the updated JS Fiddle demo here.
Additional Resources: