How to check radio button is checked using JQuery?
Asked Answered
L

9

43

I have two radio buttons in one group, I want to check the radio button is checked or not using JQuery, How ?

Lamond answered 4/1, 2012 at 10:19 Comment(3)
possible duplicate of how to get the values of the radio button which has been checked , jqueryStaford
possible duplicate of Check of specific radio button is checkedBaltic
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.Typecast
S
92

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()
Soper answered 4/1, 2012 at 10:26 Comment(0)
R
10

The following code checks if your radio button having name like 'yourRadioName' is checked or not:

$(document).ready(function() {
  if($("input:radio[name='yourRadioName']").is(":checked")) {
      //its checked
  }
});
Rahm answered 4/1, 2012 at 10:24 Comment(0)
J
4

This is best practice

$("input[name='radioGroup']:checked").val()
Jory answered 2/7, 2014 at 9:46 Comment(0)
T
4

jQuery 3.3.1

if (typeof $("input[name='yourRadioName']:checked").val() === "undefined") {
    alert('is not selected');
}else{
    alert('is selected');
}
Typecast answered 6/6, 2019 at 11:17 Comment(0)
S
1

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
    }
});
Shift answered 8/5, 2019 at 8:12 Comment(0)
C
1
var rdValue = $("input[name='radioGroup']:checked").val();
if(rdValue == '' || typeof rdValue === "undefined") {   
    console.log("not checked");
}
Cargile answered 10/5, 2023 at 6:56 Comment(0)
P
0

Check this one out, too:

$(document).ready(function() { 
  if($("input:radio[name='yourRadioGroupName'][value='yourvalue']").is(":checked")) { 
      //its checked 
  } 
});
Positivism answered 4/1, 2012 at 10:26 Comment(0)
S
0

Try this:

var count =0;

$('input[name="radioGroup"]').each(function(){
  if (this.checked)
    {
      count++;
    }
});

If any of radio button checked than you will get 1

Shanel answered 26/2, 2020 at 12:58 Comment(0)
M
0

Simply you can check the property.

if( $("input[name='radioButtonName']").prop('checked') ){
    //implement your logic
  }else{
    //do something else as radio not checked   
  }
Manage answered 12/10, 2021 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.