Find out whether radio button is checked with JQuery?
$('#element').click(function() {
if($('#radio_button').is(':checked')) { alert("it's checked"); }
});
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
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();
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!