bootstrap switch on change
To detect a change event in a Bootstrap switch, you can use the change
event handler. Here's an example of how you can implement it:
<input type="checkbox" id="mySwitch" data-toggle="switch">
$('#mySwitch').on('change', function() {
if ($(this).is(':checked')) {
// Switch is checked
console.log('Switch is checked');
} else {
// Switch is not checked
console.log('Switch is not checked');
}
});
In this example, we have an input element of type checkbox with the id mySwitch
. We attach a change
event handler to it using the on
method. Inside the event handler, we check if the switch is checked using the is(':checked')
method. If it is checked, we log "Switch is checked" to the console. If it is not checked, we log "Switch is not checked" to the console.
You can replace the console.log
statements with your own code to perform the desired actions when the switch is changed. Remember to include the Bootstrap and jQuery libraries in your project for this code to work properly.