6

I am using this bootstrap switch library.

I have a checkbox in my html. Now, it is always showing in the OFF state, even though its checked property is set to true when the document is loaded.

What do I need to do besides that?

Edit - What I understand is Switch is adding a container around input type checkbox field with class bootstrap-switch-off by default, irrespective of the value of checked attribute that goes by with the checkbox field.

<input type="checkbox" name="my-custom" id="Switch" checked="true">
jgillich
  • 71,459
  • 6
  • 57
  • 85
user2171262
  • 123
  • 1
  • 2
  • 9

3 Answers3

13

Remember that checked is a boolean attribute, meaning it's presence alone determines whether it is applied. In HTML5, you can use the empty attribute syntax as either checked or checked="", but I would caution against using checked="true", as it implies checked="false" would actually do something. Whereas checked='potato' is equally valid.

The following code should initialize bootstrap switch with checkboxes defaulted on or off:

<input type="checkbox" class="switch" checked /> <!-- on  --&gt
<input type="checkbox" class="switch" />         <!-- off --&gt
$("input.switch").bootstrapSwitch();

screenshot

Working Demo in Stack Snippets & jsFiddle

$("input.switch").bootstrapSwitch();
body { padding: 15px; }
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/js/bootstrap-switch.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/css/bootstrap2/bootstrap-switch.min.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/3.2.1/css/font-awesome.min.css">

<div >
    <label for="chk1">Default On:</label>
    <input type="checkbox" class="switch" id="chk1" checked />
</div>

<div>
    <label for="chk2">Default Off:</label>
    <input type="checkbox" class="switch" id="chk2" />
</div>

Further Reading

KyleMit
  • 30,350
  • 66
  • 462
  • 664
0

Solved this issue, and it wa s simple one at that.

I had to add 'event' to my handleChange function

handleChange: function (event, state) {
      console.log(state);
      this.setState({storyStatus: state});
  },
chinds
  • 1,761
  • 4
  • 28
  • 54
0

I solved adding "state" property in JQuery:

$("#is_demo").bootstrapSwitch('state', true);
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 26 '22 at 14:11