How To Get the Value of a Checked Radio Button in a Specific Form

How To Get the Value of a Selected Radio Button in a Specific Form: jQuery(‘input[name=radioName]:checked’, ‘#yourForm’).val()
![]() |
How To Get the Value of a Selected Radio Button in a Specific Form: jQuery(‘input[name=radioName]:checked’, ‘#yourForm’).val()
1 2 3 4 5 6 7 8 9 |
if ( ! jQuery('input[name=yourRadioGroup]').is(':checked') ) return false; ~OR~ if (! jQuery('input[name=yourRadioGroup]:checked').val()) { alert('Please make a selection...'); } else { alert('Thank you for making a selection!'); } |
Just be sure to add the class “yourClass” to each checkbox in the group you wish to count and it is as simple as:
1 |
alert( 'Qty checked: ' + jQuery('.yourClass:checked').length ); |
The jQuery
1 2 3 4 |
jQuery('#yourForm label') .live( 'click', function () { jQuery(this).attr('checked','checked'); }); |
The HTML
1 2 3 4 |
<form id="yourForm" method="post" action="/"> <label for="radio1"><input type="radio" name="fruit" value="apple" id="radio1">Click here for Apples</label> <label for="radio2"><input type="radio" name="fruit" value="pear" id="radio2">Click here for Pears</label> </form> |
To check the checkbox simply set the value of the “checked” attribute: jQuery(‘.ChkBox’).attr(‘checked’,true); To uncheck it, just remove the attribute entirely: jQuery(‘.ChkBox’).removeAttr(‘checked’);