Git Product home page Git Product logo

bootstrap-switch's Introduction

Bootstrap Switch

Dependency Status devDependency Status NPM Version

Turn checkboxes and radio buttons into toggle switches. Created by Mattia Larentis, maintained by Emanuele Marchi and Peter Stein with the help of the community. Compatible with Bootstrap 4, Bootstrap 3 and Bootstrap 2.

Play with demo on my fiddle.

Quick start

Several quick start options are available:

  • Download the latest release
  • Clone the repo: git clone https://github.com/Bttstrp/bootstrap-switch.git
  • Install with npm: npm install bootstrap-switch
  • Install with yarn: yarn add bootstrap-switch
  • Install with Composer: composer require components/bootstrap-switch
  • Install with Bower: bower install bootstrap-switch
  • Install with NuGet: PM> Install-Package Bootstrap.Switch (NuGet package)

Include the dependencies: jQuery, Bootstrap and Bootstrap Switch CSS + Javascript:

<link href="bootstrap.css" rel="stylesheet">
<link href="bootstrap-switch.css" rel="stylesheet">
<script src="jquery.js"></script>
<script src="bootstrap-switch.js"></script>

Add your checkbox:

<input type="checkbox" name="my-checkbox" checked>

Initialize Bootstrap Switch on it:

$("[name='my-checkbox']").bootstrapSwitch();

Enjoy.

Supported browsers

IE9+ and all the other modern browsers.

LESS

  • For Bootstrap 2 (no longer officially supported), import src/less/bootstrap2/bootstrap-switch.less
  • For Bootstrap 3, import src/less/bootstrap3/bootstrap-switch.less

SASS/SCSS

  • For Bootstrap 4, import src/sass/bootstrap4/bootstrap-switch.scss

Bugs and feature requests

Have a bug or a feature request? Please first search for existing and closed issues. If your problem or idea is not addressed yet, please open a new issue.

The new issue should contain both a summary of the issue and the browser/OS environment in which it occurs and a link to the playground you prefer with the reduced test case. If suitable, include the steps required to reproduce the bug.

Please do not use the issue tracker for personal support requests: Stack Overflow is a better place to get help.

Known issues

  • Make sure .form-control is not applied to the input. Bootstrap does not support that, refer to Checkboxes and radios

Integrations

License

Licensed under the MIT License.

bootstrap-switch's People

Contributors

aimeast avatar ceefour avatar engoyan avatar frapontillo avatar grahamcampbell avatar graingert avatar ickbinhier avatar julusian avatar manuelmeurer avatar marclaporte avatar markmarkoh avatar michaek avatar mtj8033 avatar nathasm avatar ncardeli avatar nostalgiaz avatar nwhite89 avatar oniram88 avatar owlycode avatar pasvaz avatar pauloortins avatar putnik avatar regepan avatar rohansakhale avatar root-talis avatar same31 avatar scavenger3 avatar t3chn0r avatar volpino avatar zstars avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bootstrap-switch's Issues

Remove attaching bootstrapSwitch to any element with class 'switch'

Was using bootstrap-switch inside one of my projects. The one issue I have is that inside bootstrapSwitch.js on line 237, you have attached the bootstrapSwitch to any element with a class of switch.

$(function () {
  $('.switch')['bootstrapSwitch']();
});

The problem I am having is that it then affects one of my other js library files ( bootstrap-datepicker.js) which use the class switch for styling element (which I find is a very generic name):

screen shot 2013-06-20 at 12 22 51

I know that if I remove the snippet from your library file it will fix this issue however I would then have to remember to remove this every time I update your script.

I propose maybe changing the name of the class to .bootstrap-switch or removing it from the library file.

Any feedback would be appreciated.

Thomas

Silly question

Looking at your samples you are using a css class "switch"...which I cannot find in the css file. I presume I am missing something obvious, but well..err... I'm missing it 👯

Auto Width

Hi,
if I use these labels the switch button doesn't show me the whole label...

data-on-label="ATTIVO" data-off-label="NASCOSTO"

maybe too long?
How can I set a auto-width?

Thanks so much!

Getting the value from input checkbox value property

Well I'm using a function to map a form and convert all the value to a javascript object, the problem using this plugin is that never update the value from the input, or mark them as checked="checked".

I think would be a good practice to add the value="true" or value="false" when the control change the status.

I made this change whit the follow line of code.

            $element.find('input').on('change', function (e) {
              var $this = $(this)

                ...
                ...
                ...

                $element.parent().trigger('switch-change', {'el': $this, 'value': thisState})
              }

              /***** ADD CODE ****/
              var checkbox = $element.find("input:checkbox");
              if ($element.attr("class").indexOf("switch-on") != -1) {
                  checkbox.attr("checked", "checked");
                  checkbox.val("true")
              } else {
                  checkbox.removeAttr("checked");
                  checkbox.val("false");
              }

              /***** ADD CODE ****/

            });

so now you can do a map like the follow.

//selected solution http://stackoverflow.com/questions/1184624/convert-form-data-to-js-object-with-jquery samuel meacham

Util.form2Json = function (inputs) {
    var listObj = {}
    $.map(inputs, function (n, i) {
        /*
        //.NET Dictionary way
        var o = {};
        o[n.name] = $(n).val();
        return o;
        */

        var o = {};
        o[n.name] = $(n).val();
        listObj[n.name] = $(n).val();
        return o;
    });
    return listObj;
}

and the value of the switch control will be true or false, like if you will use the method checked.

This modify give you more control over the swtich control, letting programmers to decide how to get the proper value for this control,

My two cents
ncubica

jQuery 1.9

Hi! Great plug-in, I just found it today, I was doing some quick tests and I was seeing some weird behavior (switching only working once or twice).

After doing some checks it seems the plug-in doesn't work well with jQuery 1.9.

It will be nice if you can check why it breaks with 1.9 and do the necessary adjustments as it seems to be one of the best implementations out there for this type of ON/OFF switch.

Regards and keep up the good work!

Using .form-horizontal .controls Bootstrap classes issue

Hello,

When using the following code:

<div class="control-group">
   <label class="control-label" for="stay_connected">Rester connecté : </label>
   <div class="switch switch-small controls" data-on-label='Oui' data-off-label='Non'>
      <input id="stay_connected" type="checkbox" value="stay_connected" />
   </div>
</div>

I can't get the switch to be aligned on the left. The controls class have a 180x left margin I can't override, dunno why.

I tried in many way. If you ever have a suggestion.

Kind regards.

EDIT: The only way I found is to use an #id, but it's not quite perfect if I have multiple switches in the same form, as an #id should be unique.

I can as well use a new div inserted:

<div class="control-group">
    <label class="control-label" for="stay_connected">Rester connecté : </label>
    <div class="controls">
        <div class="switch switch-small" id="switch" data-on-label='Oui' data-off-label='Non'>
            <input id="stay_connected" type="checkbox" value="stay_connected" />
        </div>
    </div>
</div>

In this case I can set a new margin to ".form-horizontal .controls .switch" rule but I can only alter the margin-left, margin-top is not working unlike using the #id.

Slider icon

It would be nice to be able to specify a slider icon and to have that icon be able to be bigger than the field it's sliding over. I'm trying to get the effect from the attached image and can't seem to do it without messing with the DOM elements this library injects. If I get it working I'll add a pull request.

image

Tansform a dynamically created checkbox

Can't transform a dynamically created checkbox to the bootstrapSwitch. Is this possible?


$('#test').html('Switch: <div class="switch"><input type="checkbox" checked="checked" id="mySwitch"></div>');
then run
$('#mySwitch').bootstrapSwitch();

This plugin does not work with mvc bundle optimizations

It fails and beaks our whole application simply because switch is a reserved keyword. Changed the code to use "witch" instead and then it worked perfectly. Witch is of course not a great name but you should consider a name change to give better compatibility for different circumstances.

Other than this it works perfectly and I am very greatful for a job well done! Had some problems getting it work at first because our application is built with knockout js. But after realizing that the control wasn't rendered yet when I did .switch() on it I fixed it real quick by writing a knockout binding handler that called the function when the control indeed was being set up by knockout..

Cheers for a good plugin! (checkboxes are way to ugly for modern applications!)

Enable/Disable function would be great!

Hi.

After playing around with this great component, I would like to suggest two new functions:

setEnabled(bool) and isEnabled()

Sometimes it is impossible to keep track of activation states based on toggleActivation().

Cheers

z-index on label conflicts with proprietary widgets

Hello!

I've run into an issue with bootstrap switch where the label element shows through date pickers and various components meant to overlay the page. This is caused by the following CSS entry:

.has-switch label {
...
z-index: 100;
}

One example where this occurs is jquery-ui's datepicker:

image

image

As far as I can tell, the z-index is arbitrary. Removing it does not have any effect on the toggle's behavior in IE, FF, Chrome, or Safari. Am I missing anything obvious here? Shall I send a PR?

onChange see altert value of checkbox for version 2.8

how to display an alert with the value of the checkbox in the onChange event?

<div class="success-toggle-button">
<input type="checkbox" name="pdid[]" class="toggle" value="1" checked="checked">
</div>
<div class="success-toggle-button">
<input type="checkbox" name="pdid[]" class="toggle" value="2" checked="checked">
</div>
<div class="success-toggle-button">
<input type="checkbox" name="pdid[]" class="toggle" value="3" checked="checked">
</div>
<div class="success-toggle-button">
<input type="checkbox" name="pdid[]" class="toggle" value="4" checked="checked">
</div>

$('.success-toggle-button').toggleButtons({
onChange: function($el, status, e){
console.log($el, status, e);
//alert(); display value of the checkbox changed???
}
});

No focus supported

bootstrap-switch can't check/uncheck it by keyboard. press [Tab] key will skip bootstrap-switch.

How make it focusable?

Mobile

Doesn't work in mobile

I tested in Android and Safari mobile

switch not working inside accordion-inner

....

                    <div id="mySwitch" class="boatshow switch switch-small" data-on-label="Display" data-off-label="Hide" data-on="primary" data-off="danger">
                    <input type="checkbox" />
                    </div>

....

using with radio buttons

Hello,
According to the answer given by on Stackoverflow here, we can use this with radio button groups. I'm not sure how to do it. There is no documentation about it. Is it possible to use with radio buttons? I have created a fiddle, if needed.

Thanks!

HTML Text - switch-mini

When using HTML Text in combination with switch-mini there is an empty space in the label.

bootstrap switch is breaking bootstrap-datepicker

If using bootstrap-switch together with bootstrap-datepicker together on one site, the calendar view of datepicker is broken.

There is a style inside the bootstrap-datepicker, which is called "switch", so it gets brocken by the css of bootstrap-switch... maybe you could tie up the selector to something more unique?
Bildschirmfoto 2013-02-28 um 17 40 31

Unexpected Token <

Great job! I was amused by this fancy toogle switch...

Unfortunately on my first try I've got this "Unexpected token <" message at line "4", first doc char... =/

Is there any chance I've done something wrong?

Beste Regards,

Arnaldo

Support for inverted and "default" (aka white) color

From the example, it is possible to set data-on and data-off attributes, however I'd like to be able to have "no color" on the ON and "danger" (aka red) on the OFF switch. At the moment, this does not seem to be possible.

Also, what if the toggle needs to be inverted? For dark themes?

bootstrap-switch not loading

First, great work :)
I try load JS files and show me error over bootstrap-switch.js. I try by remote and local files and nothing.

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>        
    <script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/1.3/bootstrapSwitch.min.js"></script> 

TypeError: $(...).bootstrapSwitch is not a function
$(function () {
$('.switch')'bootstrapSwitch'; <-------------
});

Please, can you helpme ?
Thank
Jorge Abrehu

Ajax causes switch double up

I am using this on a project where i load in Ajax tabs. I end up having to call this function to get them to show up.

$(function () {
  $('.switch')['switch']();
});

The issue is, this doubles up all the switches on previous tabs etc. Is there a way to detect which switches are already initialized and ignore them?

Any way to switch off others?

Is it any simple way to do next?
I have two (three, ..., n) switches and it is possible only one active state. So if I click on 'switch one', I want to set false state to 'switch two' (if it's enabled) and visa versa.
With this solution I need to click once to switch off all switches and click again to enable one of them:

$('.switch').on('switch-change', function () {
    $(this).siblings().bootstrapSwitch('setState', false);
});

I think it is common situation and must be simple ready solution :)

Tab/space keyboard traversal/control like regular forms

Fantastic extension! Love it :)

One think I was wondering about (and might as well build myself if it's not possible right now): can I include the switch in the normal flow of a form which you can traverse through by using tab. In normal forms, ordinary checkboxes can be toggled with the spacebar when they have received focus. Similarly I would like to do this with these switches.

setState without firing event?

Is there a way to set the state of the switch without triggering the event 'switch-change'?

My goal is to detect the change in one switch (the user clicks it) and set to false the state of all of the others.

Minified Version

Your plugin is awesome! Thanks to contribute!

Is possible to you create the minified version?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.