I have a form with a series of checkboxes. I want to warn the user, after they hit submit, if ALL of the checkboxes are unchecked. I'm using the following code to report all of the values of the checkboxes:
$('[id^=leg_rider]').filter(':checked');
This seems to work. However, when I try to check to see if the returned object is empty, it doesn't seem to work. This is what I'm trying:
$("#set_pref_submit").click(function() {
var legchecked = $('[id^=leg_rider]').filter(':checked');
if (!legchecked){ alert("no riders")};
});
Any suggestions are appreciated. Thanks!
On the line
You're actually checking the existence of the object, not if it's empty. Since you're using jQuery, you could use the .isEmptyObject() function to check if the object is empty.
Try this:
My html:
My JQuery Code:
Live example: http://jsfiddle.net/webwarrior/NJwv4/9/
This will give you the option to check only certain checkboxes.
hth, Shaun
You can use jQuery object's
length
property:Working Demo : http://jsfiddle.net/MrkfW/
Try this: using .change api as well so it keep the track
:)
API: http://api.jquery.com/change/