Created
April 5, 2022 11:58
-
-
Save abdullahoguk/e465c91b4adf31f8dbd7c910a8380ffc to your computer and use it in GitHub Desktop.
JS trigger input change event when value modified programmatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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