Skip to content
Advertisement

How to get value of bootstrap switch

I’m using bootstrap switch & i want to get checkbox value on click on switch. I’ve tried this but i din’t get the value. Could you check my code bellow & let me know where i did wrong

$('.make-switch').bootstrapSwitch('state');
$('#price_check').click(function () {
  //alert('Test');
  var check = $(this).val();
   console.log(check)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/css/bootstrap2/bootstrap-switch.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/js/bootstrap-switch.min.js"></script>
<div class="margin-bottom-10">
 <input type="checkbox" class="make-switch" id="price_check" name="pricing" data-on-color="primary" data-off-color="info" value="true">
  </div>

Advertisement

Answer

Tamara,

Bootstrap Switch has an onSwitchChange property that will be able to give you the state of the toggle. Please see this fiddle: https://jsfiddle.net/learnwithclyde/to2hng3f/

$("#price_check").bootstrapSwitch({
  onSwitchChange: function(e, state) { 
    alert(state);
  }
});

The above code snippet is how you would implement onSwitchChange property and get the state of the toggle control.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement