Search code examples
jquerytwitter-bootstrap-3bootstrap-switch

disabling and enabling bootstrap switch using jquery


I am using bootstrap switch and currently got stuck as there aren't any strong documentation or examples that I could get access to.

<div class="col-xs-6">
  <div class="pull-left">
     <input class="switch" type="checkbox" id="cb_project_status"  name="cb_project_status" 
        <?php if($data_p_projectstatus=="ACTIVE") echo("checked"); else echo("");?> 
        data-on-color="success" data-off-text="Inactive" data-on-text="Active" 
        data-off-color="warning" data-size="mini" disabled>
     </div> 
 </div>

On document ready of js file, I do

$("#cb_project_status").bootstrapSwitch();

and have attached in in the php page

 <link rel="stylesheet" href="css/bootstrap-switch.min.css">

I need to achieve the following functionality: Unless user enters all mandatory fields, the switch button needs to be disabled.

if($.fn.validateFormInputs()){
   // enable bootstrap switch
}else{
   // disable bootstrap switch
}

What is the jQuery command to enable/disable bootstrap switch. Thanks!


Solution

  • You can do it in many ways as below:

    DEMO

    Using options

    Type 1

    With initialization

    $("[name='my-checkbox']").bootstrapSwitch({
        disabled:true
    });
    

    Type 2

    After initialization

    $("[name='my-checkbox']").bootstrapSwitch(); //initialized somewhere
    $("[name='my-checkbox']").bootstrapSwitch('disabled',true);
    

    Using Method

    $("[name='my-checkbox']").bootstrapSwitch(); //initialized somewhere
    $("[name='my-checkbox']").bootstrapSwitch('toggleDisabled',true,true);
    

    Documentation