views:

620

answers:

1

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.

+3  A: 

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'))
}
duckyflip
what does !! stand for and why?
Sven Larson
[Duble NOT, it converts boolean value two times in a row so `!!true = true`](http://stackoverflow.com/questions/1406604/what-does-the-operator-double-exclamation-point-mean-in-javascript/1406618#1406618)
rebus