14

How can I customize bootstrap switch color to the desired color?

I found a oncolor and offcolor but not know how to change them.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Ebrahim
  • 1,740
  • 2
  • 25
  • 31

1 Answers1

26

According to this page every option is also a method. So you can apply onColor and OffColor like that:

$("#sw").bootstrapSwitch({onColor: 'danger'});

or like that:

$("#sw").bootstrapSwitch();
...
$("#sw").bootstrapSwitch('onColor', 'danger');

or to override the default options of the library:

$.fn.bootstrapSwitch.defaults.onColor = 'danger';

To use your own colors you need to create your own CSS class. For example, this class uses 'retroorange' name with your color:

.bootstrap-switch .bootstrap-switch-handle-on.bootstrap-switch-retroorange,
.bootstrap-switch .bootstrap-switch-handle-off.bootstrap-switch-retroorange {
  color: #fff;
  background: #ff9933;
}

Then you can use 'retroorange' in the usual way like that:

$("#sw").bootstrapSwitch({onColor: 'retroorange'});
NobodyNada
  • 7,529
  • 6
  • 44
  • 51
alemv
  • 1,088
  • 1
  • 14
  • 20
  • 2
    Thanks @alemv But I want to use my own colors, like this one: #ff9933! – Ebrahim Apr 11 '15 at 19:34
  • 1
    I added a method how to use your own colors to my answer. – alemv Apr 16 '15 at 17:20
  • So I should use css defined classes? – Ebrahim Apr 17 '15 at 09:50
  • Yes, see https://github.com/nostalgiaz/bootstrap-switch/blob/master/dist/js/bootstrap-switch.js#L265 – alemv Apr 17 '15 at 10:03
  • this is pretty irritating, having to define classes. To do this dynamically, I end up having to dynamically inject style tags within my code. Was that really necessary? – Kyle Baker Apr 26 '20 at 04:50
  • 8
    The link to bootstrap-switch.org is no longer valid. It either goes to a domain placeholder or, sometimes, to an evil "Your Flash plug-in is out of date" page. – Mark Wickens May 31 '20 at 22:04