Emit JS Event when data automatically added in input
Posted: 19 Sep 2017, 15:09
Hello
I have a JavaScript code that automatically adds some value in an input field.
How can I detect when the value was added in input?
If I use the 'change' event, it works when the users adds manually text in input, but not when it is added automatically with JavaScript.
Here is the script:
I have a JavaScript code that automatically adds some value in an input field.
How can I detect when the value was added in input?
If I use the 'change' event, it works when the users adds manually text in input, but not when it is added automatically with JavaScript.
Here is the script:
Code: Select all
<input type='text' name='inp1' id='inp1' />
<script>
inp1 = document.getElementById('inp1');
//register change event to #inp1
inp1.addEventListener('change', (e)=>{
alert(e.target.value);
});
//automatically add some value in #inp1 after 2 sec.
window.setTimeout(()=>{
inp1.value ='Be Happy';
}, 2000);
</script>