How to toggle between bootstrap button classes onClick?

Sonya Panich

I am using Bootstrap buttons and would like them to change color when clicked. For example, the button is originally gray (btn-default) and should change to green (btn-success) on click. I would like the button to change back to the default class when clicked again. So far I have created an if statement and added the following code, however it is only changing color once, and will not return to the default class when clicked again.

$("#critical_btn").click(function () {

    if (this.class= 'btn-default'){
        $('#critical_btn').removeClass('btn-default').addClass('btn-success ');
        $(this).addClass('btn-success').removeClass('btn-default');
    }
    else if (this.class= 'btn-success'){
        $('#critical_btn').removeClass('btn-success').addClass('btn-default ');
        $(this).addClass('btn-default').removeClass('btn-success');
    }
    });

I am fairly new to this, so any help would be greatly appreciated!

Sean Wessell

Instead of doing and if elseif statement. If you want to toggle between two classes you can use $(selector).toggleClass('firstclass secondclass') as long as one on them will be assigned in the markup. (http://api.jquery.com/toggleclass/)

jQuery would look like this:

$("#critical_btn").click(function() {
  $(this).toggleClass('btn-default btn-success');
});

Example:

https://jsfiddle.net/xsotp27s/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Trying to toggle Bootstrap Button text onclick with jquery

From Dev

jQuery toggle through Twitter Bootstrap button classes

From Dev

jQuery toggle classes between buttons in Bootstrap Button Group; set one and unset the other where other is not set

From Dev

How to toggle between glyphicon classes

From Dev

How to change button text onclick with toggle function

From Dev

How to change button text onclick with toggle function

From Dev

Switching classes onClick toggle

From Dev

How to use jQuery to randomly toggle between classes

From Dev

how to toggle between two css classes with react

From Dev

Toggle focus onclick of a button

From Dev

Toggle button onclick in react

From Dev

how to integrate a toggle button with hidden content (bootstrap)

From Dev

How to enable Toggle button navbar-toggle in bootstrap

From Dev

Javascript - Toggle Multiple Classes onclick

From Dev

Button toggle horizontal with bootstrap

From Dev

bootstrap toggle button with flag

From Dev

How to toggle the display of a division with onclick event with a button in react?

From Dev

Binding Toggle Button onClick to div

From Dev

toggle contents onclick button in angularjs

From Dev

Change toggle button image onclick

From Dev

how to toggle between two css classes view types with react

From Dev

how to toggle between two ids rather than classes in jQuery

From Dev

Toggle between classes using JavaScript

From Dev

Toggle between 2 classes in jQuery

From Dev

Toggle between pool of classes in Javascript

From Dev

Change text of Bootstrap toggle button

From Dev

Bootstrap toggle button stays grey

From Dev

Toggle bootstrap navbar using button

From Dev

Bootstrap Navbar toggle button not working

Related Related

HotTag

Archive