Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count of selected radio buttons in jquery

suppose I have groups of radio buttons like so:

<label><input type="radio" value="1" name="rdQueen" /> Scaramouche</label> <br />
<label><input type="radio" value="1" name="rdQueen" /> Will you do the</label> <br />
<label><input type="radio" value="1" name="rdQueen" /> Fandango</label> <br />

... sometime later in the page ...

<label><input type="radio" value="1" name="rdFruit" /> Mango</label> <br />
<label><input type="radio" value="1" name="rdFruit" /> Kiwi</label> <br />
<label><input type="radio" value="1" name="rdFruit" /> Potato</label> <br />

All I want to do is make sure atleast one of them from both group has been selected.. so I need to count the radiobuttons that have been checked, in this case it'll be 2.

Only, I am not sure how to do that. help please!

like image 635
LocustHorde Avatar asked Aug 02 '11 14:08

LocustHorde


2 Answers

You could do:

 var numberOfCheckedRadio = $('input:radio:checked').length
 //this gives you the total of checked radio buttons on the page
like image 66
Nicola Peluchetti Avatar answered Sep 30 '22 10:09

Nicola Peluchetti


To check for only those specific groups:

$(':radio[name="rdQueen"]:checked, :radio[name="rdFruit"]:checked').length;

Example: http://jsfiddle.net/AlienWebguy/HzfKq/

like image 27
AlienWebguy Avatar answered Sep 30 '22 11:09

AlienWebguy