Bootstrap-switch doesn´t event

14,455

Solution 1

I think your code should be working http://jsfiddle.net/PNU45/

<div class="make-switch switch-small">
  <input type="checkbox" checked="true" data-checkbox="VALUE1" class="alert-status">
</div>
<div class="make-switch switch-small">
  <input type="checkbox" checked="true" data-checkbox="VALUE2" class="alert-status">
</div>
<div class="make-switch switch-small">
  <input type="checkbox" checked="true" data-checkbox="VALUE3" class="alert-status">
</div>
<div class="make-switch switch-small">
  <input type="checkbox" checked="true" data-checkbox="VALUE4" class="alert-status">
</div>

javascript:

$('.alert-status').bootstrapSwitch('state', true);


$('.alert-status').on('switchChange.bootstrapSwitch', function (event, state) {

    alert($(this).data('checkbox'));
    //alert(event);
   // alert(state);
});

Solution 2

try this :

get state

$('.alert-status').bootstrapSwitch('state', true);

On change

$('.alert-status').on('switchChange.bootstrapSwitch', function (event, state) {
 if (state) {
    // true
 } else {
    // false
 }
   event.preventDefault();
});
Share:
14,455
Anto
Author by

Anto

Updated on June 04, 2022

Comments

  • Anto
    Anto almost 2 years

    I have the following html code (working properly changing the state of the checkbox) and I need to run an alert when the state of some checkbox is changed.

    <div class="make-switch switch-small">
      <input type="checkbox" checked="true" data-checkbox="VALUE1" class="alert-status">
    </div>
    <div class="make-switch switch-small">
      <input type="checkbox" checked="true" data-checkbox="VALUE2" class="alert-status">
    </div>
    <div class="make-switch switch-small">
      <input type="checkbox" checked="true" data-checkbox="VALUE3" class="alert-status">
    </div>
    <div class="make-switch switch-small">
      <input type="checkbox" checked="true" data-checkbox="VALUE4" class="alert-status">
    </div>
    

    I have tried the following combinations, but I can not execute the routine:

    1)

    $('.make-switch.input[type="checkbox"]').on('switchChange.bootstrapSwitch', function(event, state) {
              console.log(this); // DOM element
              console.log(event); // jQuery event
              console.log(state); // true | false
              alert(this);
    });
    

    2)

    $('input[type="checkbox"]').on('switchChange.bootstrapSwitch', function(event, state) {
              console.log(this); // DOM element
              console.log(event); // jQuery event
              console.log(state); // true | false
              alert(this);
    });
    

    3)

    $('.alert-status').on('switchChange.bootstrapSwitch', function(event, state) {
              console.log(this); // DOM element
              console.log(event); // jQuery event
              console.log(state); // true | false
              alert(this);
    });
    

    4)

    $('.alert-status').on('switchChange', function(event, state) {
              console.log(this); // DOM element
              console.log(event); // jQuery event
              console.log(state); // true | false
              alert(this);
    });
    

    5)

    /*$(".alert-status").click(function(event) {
              event.preventDefault();
              event.stopPropagation();
              alert($(this).attr('data-checkbox'));
    });*/
    

    I have searched the internet, I have consulted examples, I have used with firebug, and I find no way to capture the event. Can anyone help me?

    I found this example and have modified and work in this environment, not on my website:

    http://jsfiddle.net/sumw4/13/ (I add probeProbe class and portion of code)

  • Anto
    Anto almost 10 years
    I have pasted this code into my web and does not work. I enabled firebug to detect a possible error and can not find any errors, except the following: Error: Method does not exit state! localhost/j/jquery-1.9.1.js line 6 It is very rare and I do not get it working. The checkbox changes from ON to OFF, but does not trigger the event. Thank you very much. regards
  • Anto
    Anto almost 10 years
    It is very curious, I finally changed my files "bootstrap-switch.min.js" and "bootstrap-switch.css" by the direction of his examples and has worked for me ... Thanks @Mr.Cocococo !