Using JQuery to check if no radio button in a group has been checked
I'm using
$("input:radio[name='html_radio']").is(":checked")
And will return FALSE if all the items in the radiogroup are unchecked and TRUE if an item is checked.
if (!$("input[name='html_elements']:checked").val()) {
alert('Nothing is checked!');
}
else {
alert('One of the radio buttons is checked!');
}
if ($("input[name='html_elements']:checked").size()==0) {
alert('Nothing is checked!');
}
else {
alert('One of the radio buttons is checked!');
}
You could do something like this:
var radio_buttons = $("input[name='html_elements']");
if( radio_buttons.filter(':checked').length == 0){
// None checked
} else {
// If you need to use the result you can do so without
// another (costly) jQuery selector call:
var val = radio_buttons.val();
}