In jQuery, I'd like to select all groups of radio buttons where there are no buttons checked.
Or, is there a way in which I can select all radio button groups and iterate through the groups?
I'm dynamically adding N radio button groups to a page and will not know, before hand, what the names of the radio button groups will be.
Answer: Use the jQuery :checked selector You can simply use the jQuery :checked selector in combination with the val() method to find the value of the selected radio button inside a group.
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.
To find all radio groups:
var radio_groups = {}
$(":radio").each(function(){
radio_groups[this.name] = true;
})
to find which radio group has checked radio boxes and which hasn't:
for(group in radio_groups){
if_checked = !!$(":radio[name='"+group+"']:checked").length
alert(group+(if_checked?' has checked radios':' does not have checked radios'))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With