Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - ensuring all radio groups are checked

Tags:

jquery

radio

I'd like to loop through multiple (dynamic) radio button groups using jQuery, and if any haven't had a selection made, it throws up an error and stops the form submission.

Here's my effort so far:

$("form").submit(function() {
    $(":radio").each(function(){
        if($(this).val().length == 0) {
            alert('Not selected all radios');
            return false;
        }
    }); 
});

But it always ignores the if statement which will stop the submission, as if maybe $(this) isn't actually the value of the radio buttons?

Here's a jsFiddle: http://jsfiddle.net/aVVW9/

Any help would be much appreciated, thank you!

like image 882
Nick Avatar asked May 22 '12 17:05

Nick


People also ask

Is radio checked in jQuery?

We can check the status of a radio button by using the :checked jQuery selector together with the jQuery function is . For example: $('#el').is(':checked') . It is exactly the same method we use to check when a checkbox is checked using jQuery.

How to check if radio button group is selected?

Using Input Radio checked property: The Input Radio checked property is used to return the checked status of an Input Radio Button. Use document. getElementById('id'). checked method to check whether the element with selected id is check or not.

How to check radio button checked value in jQuery?

To check which radio button is selected in a form, we first get the desired input group with the type of input as an option and then the value of this selection can then be accessed by the val() method. This returns the name of the option that is currently selected.


2 Answers

Try this. The approach is to loop through ALL radio buttons, and THEN extract the name of the radio button group, using :checked to see if any member of that group is checked. A simple Boolean stops the errors after the first missing check is found.

$("form").submit(function() {
    var submitme = true;
    $(':radio').each(function() { // loop through each radio button
        nam = $(this).attr('name'); // get the name of its set
        if (submitme && !$(':radio[name="'+nam+'"]:checked').length) { 
        // see if any button in the set is checked
            alert(nam+' group not checked');
            submitme = false;
        }
    });
    return submitme; // cancel the form submit
});        ​

http://jsfiddle.net/mblase75/aVVW9/5/

like image 127
Blazemonger Avatar answered Sep 21 '22 12:09

Blazemonger


$("form").submit(function() {
    $(":radio", this).each(function(){
        if(!this.checked) {
            alert('Not selected all radios');
            return false;
        }
    }); 
});

or

$("form").submit(function() {
    if($(':radio:not(:checked)', this).length) {
       alert('Not selected all radios');
       return false;
    }
});

Check this demo. Here for simplicity I wrap each radio group within a div having class radio_group and loop over them.

like image 45
thecodeparadox Avatar answered Sep 23 '22 12:09

thecodeparadox