Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle Twitter Bootstrap button class when active

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.

like image 220
bretterer Avatar asked Jun 29 '12 18:06

bretterer


People also ask

How do I make Bootstrap button active?

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> .

Which class is used to set a button active and reactive in Bootstrap?

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.

How can make button Unclickable in Bootstrap?

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.

Which class should be used to indicate a button group in Bootstrap?

btn-group class is used to create horizontally arranged button groups.


1 Answers

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

like image 114
bretterer Avatar answered Sep 27 '22 18:09

bretterer