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?
Cheers, Keith
Source: Tips4all
$('#element').click(function() {
ReplyDeleteif($('#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 :
ReplyDelete$(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
You'd have to bind the click event of the checkbox, as the change event doesn't work in IE.
ReplyDelete$('#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();