If I change the value of an input field programmatically, the input
and change
events are not firing. For example, I have this scenario:
var $input = $('#myinput');
$input.on('input', function() {
// Do this when value changes
alert($input.val());
});
$('#change').click(function() {
// Change the value
$input.val($input.val() + 'x');
});
<input id="myinput" type="text" />
<button id="change">Change value</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
The problem: The event is triggered when I type in the textfield, but not when I press the button. Is there a way to achieve this with some kind of event or otherwise without having to do it manually?
What I don't want to do: I could go through all my code to add a trigger
or function call everywhere manually, but that's not what I'm looking for.
Why: The main reason I would like to do this automatically is that I have a lot of input fields and a lot of different places where I change these inputs programmatically. It would save me a lot of time if there was a way to fire the event automatically when any input is changed anywhere in my code.
input
do not exist, trykeyup
keydown
eventschange
instead ofinput
, thechange
will not fire until#myInput
is out of focus. If you want the event to fire upon clicking a button, then that would unfocus(or blur)#myInput
and thechange
event would fire in the context of the#myInput
.input
orchange
events. Is is because it isnt automatic only, or you have an additional problems with this solution? Thank youtrigger
everywhere, but it would save me a lot of time if there was a way to not having to do this. Besides that, I am just interested in the solutions people might come up with.