In twitter bootstrap, I was looking for a way to allow a button to be the default color when not active. Once a button is active, I wanted to change the color of it. I looked at ways of doing it with the js file that was provided. I ended up writting a little script to handle this.
Add data-toggle="button" to toggle a button's active state. If you're pre-toggling a button, you must manually add the .active class and aria-pressed="true" to the <button> .
The . btn class can be used in <a>, <button> and <input> elements to create button links and button tags. Example: The below programming code shows an example of a button link and various button tags.
Active/Disabled Buttons The class . active makes a button appear pressed, and the disabled attribute makes a button unclickable. Note that <a> elements do not support the disabled attribute and must therefore use the . disabled class to make it visually appear disabled.
btn-group class is used to create horizontally arranged button groups.
By adding class-toggle
as an attribute to the button equaling what you would like to go to once the button is pressed and adding the following jQuery, you will get the result described above.
$('.btn-group > .btn, .btn[data-toggle="button"]').click(function() {
if($(this).attr('class-toggle') != undefined && !$(this).hasClass('disabled')){
var btnGroup = $(this).parent('.btn-group');
if(btnGroup.attr('data-toggle') == 'buttons-radio') {
btnGroup.find('.btn').each(function() {
$(this).removeClass($(this).attr('class-toggle'));
});
$(this).addClass($(this).attr('class-toggle'));
}
if(btnGroup.attr('data-toggle') == 'buttons-checkbox' || $(this).attr('data-toggle') == 'button') {
if($(this).hasClass('active')) {
$(this).removeClass($(this).attr('class-toggle'));
} else {
$(this).addClass($(this).attr('class-toggle'));
}
}
}
});
Then the button would look like
<button class="btn" data-toggle="button" class-toggle="btn-inverse">Single Toggle</button>
If you want to do it with a button group as well it works the same way. Just add the class-toggle
to each button in the group.
The jsfiddle
UPDATE
I have modified and created a gist to have a better response when starting you button with a class. Toggle Twitter Bootstrap Classes
UPDATE
I have made a change to fix the bug that appears when you click on an active radio button
Find it here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With