When set value of input in JavaScript which event raised?
I tried these event: (propertychange
change
keypress
paste
focus
textInput
input
keyup
keydown
change
).
For example:
$('input').live/bind/on('which event', function() {
//...
});
$('input')[0].value = "sd";
When set value of input in JavaScript which event raised?
I tried these event: (propertychange
change
keypress
paste
focus
textInput
input
keyup
keydown
change
).
For example:
$('input').live/bind/on('which event', function() {
//...
});
$('input')[0].value = "sd";
Share
Improve this question
edited Oct 14, 2022 at 18:15
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Aug 27, 2013 at 12:26
Abbas BafekrAbbas Bafekr
631 gold badge1 silver badge6 bronze badges
3
-
1
Are you perhaps looking for the
change
event? – Lix Commented Aug 27, 2013 at 12:28 - Since you're doing it programatically, why not just run whatever function you need when setting the input's value? – BenM Commented Aug 27, 2013 at 12:28
-
2
No event is raised when you change the value programmatically (except
propertychange
, which is non-standard). Consider raising your own event after performing the change operation. – Frédéric Hamidi Commented Aug 27, 2013 at 12:29
3 Answers
Reset to default 5When you set the value programmatically no event will be raised. You can raise it yourself though:
$('input')[0].value = "sd";
$('input').first().trigger("change");
Or just using jQuery:
$('input').first().val("sd").trigger("change");
You can do something like this. You have to explicitly call change
event.
$('input').val('New Value').change();
When a value is changed via a script the change event is not fired, if you want to run a handler then you can trigger the event manually
$('input').eq(0).val("sd").trigger('change');