0

I don't want to listen for the event of the switch being toggled, instead I want to use JavaScript to check it's current checked state.

<input type="checkbox" name="pushtoggle" state="true" data-role="none">

I have this simple JavaScript to check the checked value:

if ($('#pushtoggle').is(':checked')){ alert('checked'); } else { alert('not checked');}

No matter which state (on or off) the switch is set to I always get the alert that it is not checked.

I initialized the toggle switch using:

$("[name='pushtoggle']").bootstrapSwitch();

I also tried setting the checked attribute within the input tag. Nothing works to get me the correct state of the switch.

2 Answers 2

10

You don't have any element with id = pushtoggle but you're using an id selector.

Try with this

if ($('[name="pushtoggle"]').is(':checked')){ 
  alert('checked'); 
} 
else { 
  alert('not checked');
}
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, stupid mistake on my part... changed name to id and it worked. Thanks Claudio.
This one works with BS 5, the others don't work
5
$("[name='pushtoggle']").bootstrapSwitch('state')

1 Comment

Thanks! This solution is good for setting too. $('#mySwitch').attr('checked', true); does nothing, .bootstrapSwitch('state',true) does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.