Mike Henley Mike Henley - 5 months ago 28
jQuery Question

Bootstrap switch button event

I'm using bootstrap switch button. I want to change input value when on text or off text is changed. How can I check if on-text="Android" I want to print android to inp1 text or if off-text="IOS" I want to print ios to inp1 text. İs there another way to check text? You can check online demo http://jsfiddle.net/PNU45/156/

HTML

<input type="input" id="inp1" value="">
<input type="input" id="inp2" value="">
<input type="checkbox" checked="true" class="alert-status" id="btn1" data-size="normal" name="my-checkbox" data-on-text="Android" data-off-text="IOS">
<input type="checkbox" checked="true" class="alert-status" id="btn2" data-size="normal" name="my-checkbox" data-on-text="Java" data-off-text="C#">


JavaScript

$('.alert-status').bootstrapSwitch('state', true);
$('#btn1').on('switchChange.bootstrapSwitch', function (event, state) {
var x=$(this).data('on-text');
var y=$(this).data('off-text');
if(x=="Android"){
$("#inp1").val("Android");
}
if(y=="IOS"){
$("#inp1").val("IOS");
}
});
$('#btn2').on('switchChange.bootstrapSwitch', function (event, state) {
var x=$(this).data('on-text');
var y=$(this).data('off-text');
if(x=="Java"){
$("#inp2").val("Java");
}
if(y=="IOS"){
$("#inp2").val("IOS");
}
});

Answer

You can check if input is checked:

$('#switch').on('switchChange.bootstrapSwitch', function (event, state) {
    var x=$(this).data('on-text');
    var y=$(this).data('off-text');
    if($("#switch").is(':checked')) {
      $("#inp1").val(x);
    } else {
      $("#inp1").val(y);
    }
});
$('#switch2').on('switchChange.bootstrapSwitch', function (event, state) {
    var x=$(this).data('on-text');
    var y=$(this).data('off-text');
    if($("#switch2").is(':checked')) {
      $("#inp2").val(x);
    } else {
      $("#inp2").val(y);
    }
});

JSFIDDLE