I have HTML like this:
<input type="radio" name="type" value="FL" checked="checked" />Fixed
<input type="radio" name="type" value="SV" />Saving
<input type="radio" name="type" value="L2" />Type 2
And following script
$(function () {
$('input[name=type]').change(function () {
alert($(this).val());
});
$('input[value=SV]').attr('checked', 'checked');
});
Firstly, have added a change event to radio button. change event handler triggered if I am selecting radio button from UI. But it does not triggered when I change selected radio button value pro-grammatically.
I want change event
also to be triggered when I select radio button pro-grammatically.
I have HTML like this:
<input type="radio" name="type" value="FL" checked="checked" />Fixed
<input type="radio" name="type" value="SV" />Saving
<input type="radio" name="type" value="L2" />Type 2
And following script
$(function () {
$('input[name=type]').change(function () {
alert($(this).val());
});
$('input[value=SV]').attr('checked', 'checked');
});
Firstly, have added a change event to radio button. change event handler triggered if I am selecting radio button from UI. But it does not triggered when I change selected radio button value pro-grammatically.
I want change event
also to be triggered when I select radio button pro-grammatically.
- You choose a bad nickname. what will you do next year...? – gdoron Commented Apr 1, 2012 at 15:14
- I will change it to my real name after a few days only. – shashwat Commented Apr 1, 2012 at 15:17
- possible duplicate of Checkbox change event not firing ... please use the search before you ask a new question. – Felix Kling Commented Apr 1, 2012 at 15:58
- @new to stackoverflow, adding a ment to every response asking for upvote isn't a good policy and won't get you more upvotes; asking a good question will. And your ments may get flagged. – Mahm00d Commented Apr 22, 2012 at 13:51
- @FlomEnol Dear, sorry for that..bt that tym I could not even vote up answers. Thats because I did it. Now I hv removed any such cmnts. Sorry again – shashwat Commented Apr 23, 2012 at 6:06
2 Answers
Reset to default 11You can use trigger()
to programmatically raise an event:
$(function () {
$('input[name="type"]').change(function() {
console.log($(this).val());
});
$('input[value="SV"]').prop('checked', true).trigger('change');
});
It won't change automatically. You have to do one of the following:
either
$('input[value=SV]').attr('checked', 'checked').trigger("change")
or
$('input[value=SV]').attr('checked', 'checked').change();