Hashcoin Plus - Bitcoin Crypto Currency Html Template With Page Builder
Viewed 926k times
573
92
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?
jquery html jquery-ui checked
shareimprove this question
edited Sep 25 '19 at 11:28
DurkoMatko
91522 gold badges66 silver badges1818 bronze badges
asked Feb 16 '10 at 11:33
Keith Donegan
23.4k3030 gold badges8686 silver badges125125 bronze badges
2
possible duplicate of Check of specific radio button is checked – Felix Kling Jul 20 '12 at 14:31
1
related Monitoring when a radio button is unchecked stackoverflow.com/questions/5824639/… – Adrien Be Jul 7 '14 at 11:55
jQuery is still popular, but if you want to have no dependencies, see below. Short & clear function to find out if radio button is checked on ES-2015: stackoverflow.com/a/55917739/9769604 – MaxWall Apr 30 '19 at 9:21
add a comment
17 Answers
activeoldestvotes
1057
$('#element').click(function(){if($('#radio_button').is(':checked')){alert("it's checked"); }});
shareimprove this answer
edited Nov 2 '11 at 11:52
answered Feb 16 '10 at 11:38
David Hedlund
116k2727 gold badges194194 silver badges209209 bronze badges
Bingo! thanks David. So would I have to invoke an action (click etc) to show the alert? Is there a way to do this without clicking? – Keith Donegan Feb 16 '10 at 11:50
3
This doesn't solve the "without the help of the click function", does it? – Znarkus Feb 16 '10 at 11:58
5
@Znarkus: OP appears satisfied. would you argue that you own use of ('#radio_button').click is without click? – David Hedlund Feb 16 '10 at 12:05
3
@David Second line should be if ($('#radio_button').is(':checked'))){alert("it's checked"); }- you forgot the jQuery $ sign and then need to wrap it all in some more parenthesis. – zuallauz Nov 2 '11 at 8:28
2
@zua: you're right! it even had a syntax error, the way it was before (unmatched brackets). fixed – David Hedlund Nov 2 '11 at 11:53
show 4 more comments
152
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!');
}});
});
send us your feedback
Austin Brain
Author
Maybe you would like to read about it

Shoppie - Responsive E-Shop Html5 Templa...
OUR CAUSES65HUNGER IN NIGERIA$2300 donated ...

Hashcoin Plus - Bitcoin Crypto Currency...
Viewed 926k times 573 92 I can set a radio...
0 comment