Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
190
rated 0 times [  197] [ 7]  / answers: 1 / hits: 19417  / 10 Years ago, wed, july 23, 2014, 12:00:00

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.


More From » jquery

 Answers
4

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');
}

[#70069] Tuesday, July 22, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hadens

Total Points: 142
Total Questions: 98
Total Answers: 100

Location: Kenya
Member since Mon, Jun 14, 2021
3 Years ago
;