Code Monkey home page Code Monkey logo

bootstrap-toggle's Introduction

Bootstrap Toggle

Bootstrap Toggle is a highly flexible Bootstrap plugin that converts checkboxes into toggles.

Visit http://www.bootstraptoggle.com for demos.

Getting Started

Installation

You can download the latest version of Bootstrap Toggle or use CDN to load the library.

Warning If you are using Bootstrap v2.3.2, use bootstrap2-toggle.min.js and bootstrap2-toggle.min.css instead.

<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet">
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>

Bower Install

bower install bootstrap-toggle

Usage

Basic example

Simply add data-toggle="toggle" to convert checkboxes into toggles.

<input type="checkbox" checked data-toggle="toggle">

Stacked checkboxes

Refer to Bootstrap Form Controls documentation to create stacked checkboxes. Simply add data-toggle="toggle" to convert checkboxes into toggles.

<div class="checkbox">
  <label>
    <input type="checkbox" data-toggle="toggle">
    Option one is enabled
  </label>
</div>
<div class="checkbox disabled">
  <label>
    <input type="checkbox" disabled data-toggle="toggle">
    Option two is disabled
  </label>
</div>

Inline Checkboxes

Refer to Bootstrap Form Controls documentation to create inline checkboxes. Simply add data-toggle="toggle" to a convert checkboxes into toggles.

<label class="checkbox-inline">
  <input type="checkbox" checked data-toggle="toggle"> First
</label>
<label class="checkbox-inline">
  <input type="checkbox" data-toggle="toggle"> Second
</label>
<label class="checkbox-inline">
  <input type="checkbox" data-toggle="toggle"> Third
</label>

API

Initialize by JavaScript

Initialize toggles with id toggle-one with a single line of JavaScript.

<input id="toggle-one" checked type="checkbox">
<script>
  $(function() {
    $('#toggle-one').bootstrapToggle();
  })
</script>

Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-on="Enabled".

<input type="checkbox" data-toggle="toggle" data-on="Enabled" data-off="Disabled">
<input type="checkbox" id="toggle-two">
<script>
  $(function() {
    $('#toggle-two').bootstrapToggle({
      on: 'Enabled',
      off: 'Disabled'
    });
  })
</script>
Name Type Default Description
on string/html "On" Text of the on toggle
off string/html "Off" Text of the off toggle
size string "normal" Size of the toggle. Possible values are large, normal, small, mini.
onstyle string "primary" Style of the on toggle. Possible values are default, primary, success, info, warning, danger
offstyle string "default" Style of the off toggle. Possible values are default, primary, success, info, warning, danger
style string Appends the value to the class attribute of the toggle. This can be used to apply custom styles. Refer to Custom Styles for reference.
width integer null Sets the width of the toggle. if set to null, width will be calculated.
height integer null Sets the height of the toggle. if set to null, height will be calculated.

Methods

Methods can be used to control toggles directly.

<input id="toggle-demo" type="checkbox" data-toggle="toggle">
Method Example Description
initialize $('#toggle-demo').bootstrapToggle() Initializes the toggle plugin with options
destroy $('#toggle-demo').bootstrapToggle('destroy') Destroys the toggle
on $('#toggle-demo').bootstrapToggle('on') Sets the toggle to 'On' state
off $('#toggle-demo').bootstrapToggle('off') Sets the toggle to 'Off' state
toggle $('#toggle-demo').bootstrapToggle('toggle') Toggles the state of the toggle
enable $('#toggle-demo').bootstrapToggle('enable') Enables the toggle
disable $('#toggle-demo').bootstrapToggle('disable') Disables the toggle

Events

Event Propagation

Note All events are propagated to and from input element to the toggle.

You should listen to events from the <input type="checkbox"> directly rather than look for custom events.

<input id="toggle-event" type="checkbox" data-toggle="toggle">
<div id="console-event"></div>
<script>
  $(function() {
    $('#toggle-event').change(function() {
      $('#console-event').html('Toggle: ' + $(this).prop('checked'))
    })
  })
</script>

API vs Input

This also means that using the API or Input to trigger events will work both ways.

<input id="toggle-trigger" type="checkbox" data-toggle="toggle">
<button class="btn btn-success" onclick="toggleOn()">On by API</button>
<button class="btn btn-danger" onclick="toggleOff()">Off by API</button>
<button class="btn btn-success" onclick="toggleOnByInput()">On by Input</button>
<button class="btn btn-danger" onclick="toggleOffByInput()">Off by Input</button>
<script>
  function toggleOn() {
    $('#toggle-trigger').bootstrapToggle('on')
  }
  function toggleOff() {
    $('#toggle-trigger').bootstrapToggle('off')  
  }
  function toggleOnByInput() {
    $('#toggle-trigger').prop('checked', true).change()
  }
  function toggleOffByInput() {
    $('#toggle-trigger').prop('checked', false).change()
  }
</script>

Integration

A binding for knockout is available here: aAXEe/knockout-bootstrap-toggle

Demos

Visit http://www.bootstraptoggle.com for demos.

bootstrap-toggle's People

Contributors

aaxee avatar ingro avatar jhogendorn avatar minhur 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

bootstrap-toggle's Issues

Any reason why bootstrapToggle wouldn't work with Ember.js?

Hello - I've been trying to get this to work with our Ember App, and I've been able to integrate other bootstrap plugins as per the usual method: include the library after including bootstrap, initialise via jquery if needed, and they work. However, both bootstrap toggle and bootstrap switch don't ever show up; they don't seem to apply themselves to the classes I need them to.

Are you aware of any issues with Ember.js? If not, then there must be something in our application that's preventing it.

IE8 support

I know - IE8. Unfortunately it's still used a lot.
Any chance of better support?

In IE8 the checkbox appears as a coloured rectangle with no text.

Overlaping

I have downloaded bootstrap-toggle from Nuget and have tried one of the simple examples, the buttons are overlapped, using MVC 5.0, anyone else have the same issue?

<input id="toggle-event" data-toggle="toggle" type="checkbox">
<div id="console-event"></div>
 <script>
     $(function () {
         $('#toggle-event').change(function () {
             $('#console-event').html('Toggle: ' + $(this).prop('checked'))
         })
    })
</script>

And then the toggle look like this:

untitled

Preventing the change event

I'm working with bootstrap-toggle to trigger a backend API call. What I'd like is to disable the toggle, make the call, and update the toggle based on the result (success/failure).

I've tried things like this:

$('#toggleElement').change(function(e) {
    e.preventDefault();
    e.stopPropagation();
    e.stopImmediatePropagation();
});

And this:

$(document).on('click.bs.toggle', 'div[data-toggle^=toggle]', function(e) {
    console.dir('click');
    e.preventDefault();
});

But nothing seems to work. Any thoughts on how I can disable the UI change until I explicitly invoke it?

Thanks!

Can you release LESS files

Hi,

I'm working on template for bootstrap and i need LESS file to esasy customize the controls appearance.

Can you release that files too?

Reset not working

The toggle checkboxes do not work with the standard html input type="reset".

How to get it worked with Angular directives?

I'm trying to use bootstrap-toggle with AngularJs:

<input data-toggle="toggle" type="checkbox" ng-checked="myProperty" ng-model="myProperty" ng-change="myPropertyChange()" >

But when the toggle state changes the Angular model is not updated.

Dynamic width

Is there any way to make toggles comply with the Bootstrap grid system for width? I would like my toggles to dynamically get the width of their parent div (i.e. "col-sm-6").

not working in ios

Hi this toggle button is not working in ios when loaded with meteor run ios-device.

<template name="timePicker">
  <div class="form-group">
    <div class='input-group date' id='datetimepicker3'>
      <span class="input-group-addon">
        <span class="glyphicon glyphicon-time"></span>
      </span>
      <input type='text' class="form-control" >
    </div>
  </div>

  <input id="noti-toggle" type="checkbox" data-toggle="toggle" >
</template>
Template.timePicker.onRendered(function() {

  this.$('#datetimepicker3').datetimepicker({
    format: 'LT'
  });
  this.$('#datetimepicker3 input').val("10:00 AM");
  this.autorun(function(){
    $('#noti-toggle').bootstrapToggle();
  });

});

Stacking events 'click' when creating the toggle through JS

Hi,
I am using datatables JQuery plugin and bootstrap-toggle together in an one page application.

I create the datatable (ajax loaded) and then I add the toggle functionality to the checkboxes in a column using the following javascript every time the table is loaded:

$('.checkboxes_class').bootstrapToggle({
  "size": 'mini',
  "on": "On",
  "off": "Off"
});

It works fine when I draw the datatable for the first time.
When I click on other tab I am destroying the table DOM and container and re-loading it if the user click on the tab again.

That is where the problem starts. If I click on a second tab and click again on the first apparently bootstrap/jquery is stacking events so it turn on and off. If I repeat the process it turn on, off and on again. And so on.

I tried destroying the component before creating it again, I also tried disable and enable after creating it, but nothing seems to work.

$('.checkboxes_class').bootstrapToggle('destroy');
$('.checkboxes_class').bootstrapToggle('disable');

Any light?

Thanks in advance,

Vitor Munhoz

Add navbar usage example

Could you please add some examples of using within a navbar - especially with the different size modes...

Default state of the toggle

After a toggle, for example from on to off, if we reload the page, the toggle is not reset to its previous state.
Removing the class ".off" at the ready bugs.

Change event triggered on initial render

It looks like the initial render of the toggle is firing a change event.

This can be prevented by adding a silent parameter to update(), on() and off() (in a similar way to the trigger() method) which prevents the change being triggered when called from the render method.

Checkbox Title as Title of the toggle

Hi,

I recently had the requirement to add a simple tooltip to the toggles, so i tried to add a title.
But the toggle doesn't take the title, so I created this little script, that takes the title from the checkbox and pushes it onto the div.

$.each($('input[type="checkbox"][title]'), function (sender) {
        $('#' + $(this).attr("id") + ' + .toggle-group').attr("title", $(this).attr("title"));
    });

Is there a better way?

regards,
CarnVanBeck

toggle button is not working in gwt

I'm using Bootstrap v2.3.2,bootstrap2-toggle.min.js and bootstrap2-toggle.min.css but in jsp pages it's working in gwt pages not working. It is showing only checkbox.
Please help me!

Setting Autopostback=true gets ignored

I have a requirement when the user checks the checkbox that the page autopostbacks and my .net function handler runs...this works for my normal checkbox but as soon as i add the toggle the autopost back stops...is this by design?

Click function

Why the click function don't works with this toggle? I need to use a function like this :
$('checkbox').click(function(){

var valor = $(this).attr('id');

});

how can i use this?

Old version in CDN

Hi.
It seems like there is old version of plugin(without change event) in CDN.
Can you update it?

Thx.

Set Custom Widths and Spacing?

How do I set custom Widths on the toggle?

I have a couple with long titles and they are being cut off. I also noticed that I must put a space after (On state) and before (Off state) to ensure proper spacing.

Thanks

Sizes and animation not working

Just dropped the css and js files in my Bootstrap 3 projects, two things are weird:

  1. The switch works, but doesn't animate, just switches.
  2. The 'switch' is stretching to the height of it's container. When I inspect the div your js creates, it's got inline width and height so I can't target it with css. I've got the mini directive on my checkbox but it's being totally ignored.

Any ideas?

Doesn't work within bootstrap tabs + knockout model

It seems that your component doesn't work with knockout properly. If I have wrapper like this:

It doens't work. It shows styled buttons, but 'toggle' part doesn't work.
More over this div is part of bootstrap tabs and first this tab is hidden, data into MyModel are loadd only when user is navigating to the tab. So it happens dynamically after document is ready. I guess this causes some problems. I am not sure.

Toggles inside tabs resulting in 0px height

Any toggles not on the first tab of a tab set are somehow getting set with 0px height. A toggle on the first tab looks like this:

<div class="toggle btn off btn-sm btn-default" data-toggle="toggle" style="width: 100%; height: 19px;">

A toggle on subsequent tabs looks like this:

<div class="toggle btn btn-sm btn-default off" data-toggle="toggle" style="width: 100%; height: 0px;">

Both results come from identical "data-" markup.

Impossible to set custom width using CSS

If you have to use your beautiful toggle button with a custom width set using CSS like this:

.toggle {
    width: 400px;
 }

The toggle button will not calculate its width from the right outerWidth value.

Here is the part of the code which causes the problem:

var width = Math.max($toggleOn.outerWidth(), $toggleOff.outerWidth())+($toggleHandle.outerWidth()/2)
var height = Math.max($toggleOn.outerHeight(), $toggleOff.outerHeight())        
$toggleOn.addClass('toggle-on') // HERE WIDTH IS ACTUALLY UPDATED BUT ITS TOO LATE
$toggleOff.addClass('toggle-off')       
this.$toggle.css({ width: width, height: height })

So the fix is very simple, juste move addClasses calls before width/height calculation:

$toggleOn.addClass('toggle-on')
$toggleOff.addClass('toggle-off')

before the width/height calculation, like that:

$toggleOn.addClass('toggle-on')
$toggleOff.addClass('toggle-off')   
var width = Math.max($toggleOn.outerWidth(), $toggleOff.outerWidth())+($toggleHandle.outerWidth()/2)
var height = Math.max($toggleOn.outerHeight(), $toggleOff.outerHeight())    
this.$toggle.css({ width: width, height: height })

Then everything will work fine :)

$('#toggle-event').change(function () not calling for other toggle button in grid view/datatable rows.

Hi,
I have grid view control and there are 10 rows in it...last column in grid have toggle check box control to active/inactive..when i toggle first toggle button in first row toggle change event is firing but when i click on other 9 rows toggle button ,it doesnt call toggle change event.
code:

grid view:

<asp:templatefield headertext="Toggle" >

                                <input id="toggle-event" type="checkbox" data-toggle="toggle" data-on="Active" data-off="InActive" data-size="small" >
                                   </itemtemplate>
                            </asp:templatefield>

$(function () {
$('#toggle-event').change(function () {
alert('toggle clicked');
})
});
Can you please help me out .

Why don't you have a CSS file with the toggle button designs?

I'm to install this on my site, but I noticed I need to grab all css information out of the almost unreadable: https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css
That doesn't seem right?

How come this CSS is not in the local code? Or atleast an example.css or something?

Perhaps I don't understand something, that could always be the case.
I could ofcourse import the whole https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css but that breaks my site totally, with all kind of other css not related to the toggle button.

Usage with Meteor

When trying to use in a Meteor template, it's not the actual <input>, but a surrounding <div> that changes classes.
I.e. from

<div class="toggle btn ios btn-primary" data-toggle="toggle" style="width: 58px; height: 34px;"><input id="activation" type="checkbox" checked="" data-toggle="toggle" data-style="ios"><div class="toggle-group"><label class="btn btn-primary toggle-on">On</label><label class="btn btn-default active toggle-off">Off</label><span class="toggle-handle btn btn-default"></span></div></div>

to the same, except

<div class="toggle btn ios btn-default off" ...

So the checked boolean attribute isn't add-/removed, etc. How would you make the event listeners work in this case?

If you look at the same example, it won't initialize a normal checkbox programatically either.

How can I change text on the fly?

First of all - great component!!!

Is there a way to change the on-text and off-text on the fly?

Like ...

$('#toggle-demo').bootstrapToggle('on-text', 'bla bla');

:)

Using a method reset the parameters

If any method is used like bootstrapToggle("off") or bootstrapToggle("toggle"), it removes the parameters on the input tag like data-style, data-on, data-off, data-onstyle...

Silently change toggle value

Is there anyway to programmatically update the toggle using bootstrapToggle("on")/bootstrapToggle("off") and not have it trigger the 'change' event on the checkbox?

I see in-code there is a 'silent' that determines if 'trigger()' should be called, but I'm not sure how to utilise it.

Basically I display a bootstrap modal window, the window has a series of toggles on it, on the 'show.bs.modal' event I make an AJAX request to find out what states they should be set to and then utilise one of the 2 above bootstrapToggle functions, however I don't want it to trigger the change event in that instance, only if a person physically changes the toggle.

Button Style not like demo

Anyone else have this issue? Other bootstrap items show correctly.

I'm missing the white/grey bar on either side to make it look like a slider, and the text of the off state. I'm using the JS to init() and html5 data vars for options.

I've tried a few BS classes and they all fail the same way so it's not just danger.

buttons

data-toggle to work with other implementation

I want to use data-toggle in conjunction with other java script implementation e.g. when state is "On" I want to do some other stuff like show / hide / collapse pane below check toggle checkbox.
`

` http://getbootstrap.com/javascript/#collapse

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.