When I was working on one of my projects I suddenly landed up in a situation where I wanted to find out as to which radio button has been selected or checked. So having discovered that, I am going to present you with different ways in which you can find out whether a radio button has been checked or not:
1) Here in the below line of jQuery code you can see that it will give you the status of your radio button. This code fragment will find out whether the “Checked” property of the radio button has been tick marked or not and based on the output with return either true or false. One point here to be noted is that the below line of code will function properly with jQuery Version 1.7.2 and higher:
var isChecked = $('#rdSelect').prop('checked');
Nevertheless if you are not having the latest version of jQuery installed then you must use the below code fragment to select the value of a radio button :
var isChecked = $('#rdSelect').attr('checked')?true: false;
I happened to read on different sources that the “Checked” attribute of a radio button will return either true or false if it is used with the “attr” method however this is not so true because if you checked the checked box then the result will give you the status as checked otherwise it will give the status to be undefined. So if you want the result to be either true or false in that case make use of the conditional expression that is mentioned in the above code fragment.
2) One more method to get the value of the selected radio button is to use the
$('#rdSelect: checked').Val()
method which will return undefined if the radio button is not checked and return “On” if it is checked.
Here is the code fragment to be used :
var isChecked = $('#rdSelect: checked').Val()?true: false;
3) Another simple way is to make use of the is() selector so that it will return either true or false based on the status of the radio button.
Here is the code fragment to be used :
var isChecked = $('#rdSelect').is(‘checked’);