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
JavaScript
x
6
1
$('.make-switch').bootstrapSwitch('state');
2
$('#price_check').click(function () {
3
//alert('Test');
4
var check = $(this).val();
5
console.log(check)
6
});
JavaScript
1
6
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/css/bootstrap2/bootstrap-switch.css" rel="stylesheet"/>
3
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.4/js/bootstrap-switch.min.js"></script>
4
<div class="margin-bottom-10">
5
<input type="checkbox" class="make-switch" id="price_check" name="pricing" data-on-color="primary" data-off-color="info" value="true">
6
</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/
JavaScript
1
6
1
$("#price_check").bootstrapSwitch({
2
onSwitchChange: function(e, state) {
3
alert(state);
4
}
5
});
6
The above code snippet is how you would implement onSwitchChange
property and get the state
of the toggle control.