Skip to content

Instantly share code, notes, and snippets.

@abdullahoguk
Created April 5, 2022 11:58
Show Gist options
  • Save abdullahoguk/e465c91b4adf31f8dbd7c910a8380ffc to your computer and use it in GitHub Desktop.
Save abdullahoguk/e465c91b4adf31f8dbd7c910a8380ffc to your computer and use it in GitHub Desktop.
JS trigger input change event when value modified programmatically
/*
JS ile input value değiştirildiğinde input için change eventini trigger et
JS trigger input change event when value modified programmatically
eg: input.value="bla bla"
*/
const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(prevInput), 'value');
Object.defineProperty(prevInput, 'value', {
set: function(t) {
//return descriptor.set.apply(this, arguments);
/*
önce değeri değiştir sonra change eventini trigger et
newValue = arguments[0];
oldValue = this.value;
*/
//change value
descriptor.set.apply(this, arguments);
//do something after value change like trigger change event
var event = new Event('change');
this.dispatchEvent(event);
},
get: function() {
return descriptor.get.apply(this);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment