I can easily grab the value
or check if a radio button is selected using the .click(function()
but what I need to do if check to see if a radio button is selected and/ or grab its value after a page has loaded (i.e. some radio buttons are pre-selected on page load).
I assumed something like this might work but it doesn't:
$("input[type=radio][name=q22]:checked").val()
Any suggestions?
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.
To check which radio button is selected in a form, we first get the desired input group with the type of input as an option and then the value of this selection can then be accessed by the val() method. This returns the name of the option that is currently selected.
And you wanted to check the one with a value of "Male" onload if no radio is checked: $(function() { var $radios = $('input:radio[name=gender]'); if($radios.is(':checked') === false) { $radios. filter('[value=Male]'). prop('checked', true); } });
$('#rdSelect:checked'). val() method returns "on" when radio button is checked and "undefined", when radio button is unchecked. var isChecked = $('#rdSelect').is(':checked'); The above method uses "is" selector and it returns true and false based on radio button status.
Try with:
$(document).ready(function(){
$("input[type=radio][name='q22']:checked").val()
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With