When using JQuery selectors and trying to get the value of a checkbox, it seems that it will always return the result “on”. That is because you need to check to see if it is “:checked” instead of trying to grab the value of it.
if($("input[name='YourCheckboxName']").is(":checked")){
alert('on');
}else{
alert('off');
}
UPDATE!! A plugin is now available for this tutorial that includes more features read more...
How often do you work with tables? I work with them quite a bit and styling t read more...
When running a site with a boat load of javascript, you may want to run a script read more...
Download Plugin THEMES: theme-lighttheme-dark<link href="{{ path to js read more...
Greetings All! Since everyone was digging the Star Comment Rating I decided that read more...
werwer
Ahh, this is what I suspected, thanks for the confirmation and example
And when you’re sending json data with an ajax request, it’s easy to just send the
$(selector).is(“:checked”) instead of .val() when working with checkboxes
Just thanks.
I waste a lot of time to understand this, until I see your blog.
Thanks
Thanks for the props, I threw it together rather quickly. I used this web background maker for the images that I talk about in this post: http://eligeske.com/cool-web-stuff/fast-web-background-patterns/
!!!! THANK YOU !!!!
Thanks a lot, I wasted several hours with debugging and researching on that problem. I assumed that (“#myCheckbox”).val() either returns “on” or “off”, depending on the status of the checkbox…
Thanks for the tip, fixed my problem.
Editorial: I don’t think the way this is implemented in jQuery is very elegant. Why do they design it so the checkbox needs all of this special handling? A checkbox can have no other values except for true/false, seems like the val() method should return that boolean state by default. *Groan*.
Hey Evan,
That is what the HTML value is, “on” if it is checked. So we should take this up with w3c!
I’m right there with ya on that one Evan! You tell me when the rally is to change the value and I’ll be there.