Shaun Mccran

My digital playground

13
F
E
B
2012

Using JQuery to check Radio button selections

Ever wanted to check if a user has checked at least one option from a series of radio fields? JQuery makes this super simple, here's how to do it:

Our html page contains a series of radio input fields and I want to make sure that the user has selected either 'Yes' or 'No' for each option. I know that there are thirteen radio fields, so using the JQuery selector below we can select and count the number of checked fields. If it doesn't match the total then we know the user has left something unchecked.

view plain print about
1function countChecked() {
2        var n = $("input:checked").length;
3        if(n == 13)
4            {
5                //alert(n)
6            }
7            else
8            {
9                // not enough options selected
10                alert('Please select Yes or No for each option');
11                return false;
12            }
13        }
14
15    $(".continue-button").click(countChecked);

I am using a button with a class of 'continue-button' to trigger the function.

JQuery selectors can be amazing powerful, read on them here: http://api.jquery.com/category/selectors/

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Back to top