Sona Shetty
Sona Shetty

Reputation: 1047

Issue with state of toggle switch

I have alraedy gone through many posts regarding the state change event of bootstrap switch.I am posting this question again as non of them helped me in solving the issue faced in my script.

I have created a toggle switch by taking inputs from the post given in the below link. https://codepen.io/anon/pen/OzQXRm?editors=1111

I want to get the state of the switch(On/OFF) upon clicking on the switch.

I have tried the below code as suggested in many post,but this event itself is not triggered

$('.switch').on('switchChange.bootstrapSwitch', function (event, state) {  
        //actions here          
    });    
</script>

On click event is triggered with the below code -

 $(document).on( 'click', 'input', function (e) { 
    //code here    
 });

But i am not sure how to capture the state of the toggle(on/OFF) using the above event.

Can you please help me to find a solution for this problem?

Upvotes: 2

Views: 161

Answers (2)

Sona Shetty
Sona Shetty

Reputation: 1047

I am able to check the state using the below code. Thanks Rory & Carsten for the input.

$('.switch').on('change', function (event, state) {  
  console.log($(this).is(':checked'));
});

Upvotes: 1

Jinesh
Jinesh

Reputation: 1580

Follow this code

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

    console.log($(this).val());
    alert(event);
    alert(state);
});

Upvotes: 0

Related Questions