I have already read Why onchange doesn't work? and now I know that,
The onchange event only fires if the user changes the value of the input. It isn't supposed to fire if the input is changed programmatically.
But I have an input field which is automatically filled when user change a location by Google map widget inside a website. When it happens, I want to get that automatically filled value and fill another input filed. How can I detect or fire function when input is changed programmatically?
You could achieve this by overriding the setter function of the input's value
property. So every time anyone sets the value
programmatically your overriden setter will be triggered:
const input = document.getElementById('input');
const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(input), 'value');
Object.defineProperty(input, 'value', {
set: function(t) {
console.log('Input value was changed programmatically');
return descriptor.set.apply(this, arguments);
},
get: function() {
return descriptor.get.apply(this);
}
});
function changeInput() {
input.value += '1';
}
<input id="input">
<button onclick="changeInput()">Change</button>
To trigger an event programmatically, you can use the Event API : https://developer.mozilla.org/fr/docs/Web/API/EventTarget/dispatchEvent
Full example :
let input = document.getElementById('a')
// Listen the event
input.addEventListener('change', function (evt) {
console.log('onchange event listener')
});
input.onchange = () => {
console.log('onchange callback')
}
setTimeout(() => {
input.value = "myvalue"
// Trigger the event
var event = new Event('change');
input.dispatchEvent(event);
}, 1000)
<input id="a">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With