Using JQuery how to check if any checkboxes are checked and then set the first to checked if none are checked.
var Checked = $('.ProductImageGallery').find(".DfaultCheckbox:selected);
if (Checked == null) { $('.DfaultCheckbox:first').attr('checked', true) }
You can select checked checkboxes with the
:checkedselector, and then test the.lengthof the returned jQuery object to see if any elements matched:Further reading: http://api.jquery.com/category/selectors/
Note: I’ve used the
.prop()method rather than.attr(), but.prop()only works if you’re using jQuery 1.6+ (otherwise stick with.attr()).