Return to Snippet

Revision: 80627
at March 23, 2020 06:08 by chrisaiv


Updated URL
https://www.chrisjmendez.com/2008/09/21/jquery-ensure-that-at-least-one-checkbox-is-selected/

Updated Code
https://www.chrisjmendez.com/2008/09/21/jquery-ensure-that-at-least-one-checkbox-is-selected/

Updated Description
https://www.chrisjmendez.com/2008/09/21/jquery-ensure-that-at-least-one-checkbox-is-selected/

Revision: 80626
at March 23, 2020 06:07 by chrisaiv


Updated URL
https://www.chrisjmendez.com/2020/03/23/jquery-ensure-that-at-least-one-checkbox-is-selected/

Updated Code
https://www.chrisjmendez.com/2020/03/23/jquery-ensure-that-at-least-one-checkbox-is-selected/

Updated Description
https://www.chrisjmendez.com/2020/03/23/jquery-ensure-that-at-least-one-checkbox-is-selected/

Revision: 8428
at September 20, 2008 17:13 by chrisaiv


Initial Code
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");

Initial URL

                                

Initial Description
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.

Initial Title
JQuery: Ensure that at least one checkbox is selected

Initial Tags
jquery, validation

Initial Language
JavaScript