I have three buttons, and want to change the color of the button on being selected. As you will see, I am able to change the color on selection, but I need buttonA to return to its original color when buttonB is selected (at which point buttonB should take on the selected color) and so on. Currently each button selected takes on the selected color, but buttons do not return to their original color.
Please see this fiddle: http://jsfiddle.net/Fwqv8/
This is the script:
jQuery(document).ready(function(){
jQuery('button.account').click(function() {
jQuery(this).removeClass("account");
jQuery(this).addClass("btn-success");
});
});
Ideally there will be a default button (buttonA) which has the selected color when the page loads initially.
Help would be greatly appreciated. Thank you.
YOU CAN USE
TOGGLECLASS
FOR SINGLE BUTTONLIVE DEMO
JS
CSS
HTML
Try @ codebins
You can use:
This code does all your buttons switch back to their default state on click. Only the clicked button will stay (in your case) "green".
An default button can easily been done by setting the default class in your markup.
You could remove the btn-success class on all buttons in the onclick function, then add the class only to the newly selected
Or you could have an array with the currently selected buttons, and removing the btn-success class on a newly clicked button
http://jsfiddle.net/Fwqv8/3/