Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 260
Tamara Avatar asked Dec 10 '22 03:12

Tamara


2 Answers

I guess all these solutions are old news as of v5?
in any case none of these work for me. So I used this solution

$('#orderSwitch').on('change', function() {
    console.log($(this).is(':checked'));
});
like image 80
Sagive Avatar answered Dec 17 '22 12:12

Sagive


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.

like image 39
Clyde D'Souza Avatar answered Dec 17 '22 13:12

Clyde D'Souza