Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery and radio button groups

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.

like image 714
jbenckert Avatar asked Jul 22 '09 14:07

jbenckert


People also ask

How to get radio button value with jQuery?

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.

How do you check radio button is checked or not?

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.


1 Answers

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'))
}
like image 184
duckyflip Avatar answered Oct 01 '22 01:10

duckyflip