I have two radio buttons in one group, I want to check the radio button is checked or not using JQuery, How ?
-
2possible duplicate of how to get the values of the radio button which has been checked , jquerymatino– matino2012-01-04 10:22:03 +00:00Commented Jan 4, 2012 at 10:22
-
possible duplicate of Check of specific radio button is checkedFelix Kling– Felix Kling2012-07-20 14:31:53 +00:00Commented Jul 20, 2012 at 14:31
-
Mentioned questions in the above comments are asking how to get the checked value and check specific value has choosed, not for validate whether it's checked or not. So, it's different.Sadee– Sadee2019-06-06 11:20:55 +00:00Commented Jun 6, 2019 at 11:20
Add a comment
|
9 Answers
Given a group of radio buttons:
<input type="radio" id="radio1" name="radioGroup" value="1">
<input type="radio" id="radio2" name="radioGroup" value="2">
You can test whether a specific one is checked using jQuery as follows:
if ($("#radio1").prop("checked")) {
// do something
}
// OR
if ($("#radio1").is(":checked")) {
// do something
}
// OR if you don't have ids set you can go by group name and value
// (basically you need a selector that lets you specify the particular input)
if ($("input[name='radioGroup'][value='1']").prop("checked"))
You can get the value of the currently checked one in the group as follows:
$("input[name='radioGroup']:checked").val()
Comments
Radio buttons are,
<input type="radio" id="radio_1" class="radioButtons" name="radioButton" value="1">
<input type="radio" id="radio_2" class="radioButtons" name="radioButton" value="2">
to check on click,
$('.radioButtons').click(function(){
if($("#radio_1")[0].checked){
//logic here
}
});