Home » Jquery » Find out if radio button is checked with JQuery?

Find out if radio button is checked with JQuery?

Posted by: admin November 2, 2017 Leave a comment

Questions:

I can set a radio button to checked fine, but what I want to do is setup a sort of ‘listener’ that activates when a certain radio button is checked.

Take, for example the following code:

$("#element").click(function()
{ 
    $('#radio_button').attr("checked", "checked");
});

it adds a checked attribute and all is well, but how would I go about
adding an alert. For example, that pops up when the radio button is checked
without the help of the click function?

Answers:
$('#element').click(function() {
   if($('#radio_button').is(':checked')) { alert("it's checked"); }
});

Questions:
Answers:

If you have a group of radio buttons sharing the same name attribute and upon submit or some event you want to check if one of these radio buttons was checked, you can do this simply by the following code :

$(document).ready(function(){
  $('#submit_button').click(function() {
    if (!$("input[name='name']:checked").val()) {
       alert('Nothing is checked!');
        return false;
    }
    else {
      alert('One of the radio buttons is checked!');
    }
  });
});

Source

jQuery API Ref

Questions:
Answers:

As Parag’s solution threw an error for me, here’s my solution (combining David Hedlund’s and Parag’s):

if (!$("input[name='name']").is(':checked')) {
   alert('Nothing is checked!');
}
else {
   alert('One of the radio buttons is checked!');
}

This worked fine for me!

Questions:
Answers:

You’d have to bind the click event of the checkbox, as the change event doesn’t work in IE.

$('#radio_button').click(function(){
    // if ($(this).is(':checked')) alert('is checked'); 
    alert('check-checky-check was changed');
});

Now when you programmatically change the state, you have to trigger this event also:

$('#radio_button').attr("checked", "checked");
$('#radio_button').click();

Questions:
Answers:

//Check through class

if($("input:radio[class='className']").is(":checked")) {
     //write your code         
}

//Check through name

if($("input:radio[name='Name']").is(":checked")) {
         //write your code         
}

//Check through data

if($("input:radio[data-name='value']").is(":checked")) {
         //write your code         
}

Questions:
Answers:

Another way is to use prop (jQuery >= 1.6):

$("input[type=radio]").click(function () {
    if($(this).prop("checked")) { alert("checked!"); }
});

Questions:
Answers:

If you don’t want a click function
use Jquery change function

$('#radio_button :checked').live('change',function(){
alert('Something is checked.');
});

This should be the answer that you are looking for.
if you are using Jquery version above 1.9.1
try to use on as live function had been deprecated.

Questions:
Answers:

… Thanks guys… all I needed was the ‘value’ of the checked radio button where each radio button in the set had a different id…

 var user_cat = $("input[name='user_cat']:checked").val();

works for me…

Questions:
Answers:

The solution will be simple, As you just need ‘listeners’ when a certain radio button is checked. Do it :-

if($('#yourRadioButtonId').is(':checked')){ 
// Do your listener's stuff here. 
}

Questions:
Answers:

try this

    if($('input[name="radiobutton"]:checked').length == 0) {
        alert("Radio buttons are not checked");
    }

Questions:
Answers:

dynamic generated Radio Button Check radio get value

$("input:radio[name=radiobuttonname:checked").val();

On change dynamic Radio button

$('input[name^="radioname"]').change(function () {if (this.value == 2) { }else{}});

Questions:
Answers:

Working with all types of Radio Buttons and Browsers

if($('#radio_button_id')[0].checked) {
   alert("radiobutton checked")
}
else{
   alert("not checked");
}

Working Jsfiddle Here

Questions:
Answers:

Try this:

alert($('#radiobutton')[0].checked)

Questions:
Answers:

$(‘.radio-button-class-name’).is(‘checked’) didn’t work for me, but the next code worked well:

    if(typeof $('.radio-button-class-name:checked').val() !== 'undefined'){
     // radio button is checked
    }

Questions:
Answers:
$("#radio_1").prop("checked", true);

For versions of jQuery prior to 1.6, use:

$("#radio_1").attr('checked', 'checked');