jQuery: Ensure that at least one checkbox is selected
Ok so here's the scenario, Suppose you have some sort of form with a couple of checkboxes and you want the user to select at least one before submitting, rather than write a bunch of iteration code, jQuery offers a really easy way to do it.
function setHiddenAction(element) {
var okToSubmit = false;
//A. Make sure that the user selects at least one item before they Request an Action
if( $("input:checkbox:checked").length > 0 ) okToSubmit = true;
//B. Set the Hidden Value to the Users Requested Action
$("#action").attr("value", element.value)
//C. Either Submit or give an Error Message
if( okToSubmit ) {
$("form").submit();
} else {
alert( "Please select at least one item" );
}
}
setHiddenAction("email");