Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
83
rated 0 times [  88] [ 5]  / answers: 1 / hits: 51863  / 7 Years ago, thu, february 23, 2017, 12:00:00

What is the event when an <input> element's value is changed via JavaScript code? For example:



$input.value = 12;


The input event is not helping here because it's not the user who is changing the value.



When testing on Chrome, the change event isn't fired. Maybe because the element didn't lose focus (it didn't gain focus, so it can't lose it)?


More From » events

 Answers
2

Based on @t-j-crowder and @maciej-swist answers, let's add this one, with .apply function that prevent infinite loop without redefining the object.



 function customInputSetter(){

var descriptor = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, value);
var originalSet = descriptor.set;

// define our own setter
descriptor.set = function(val) {
console.log(Value set, this, val);
originalSet.apply(this,arguments);
}

Object.defineProperty(HTMLInputElement.prototype, value, descriptor);
}

[#58797] Wednesday, February 22, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jonrened

Total Points: 627
Total Questions: 114
Total Answers: 99

Location: Zimbabwe
Member since Thu, Jul 21, 2022
2 Years ago
jonrened questions
Mon, Nov 2, 20, 00:00, 4 Years ago
Tue, May 19, 20, 00:00, 4 Years ago
Tue, Jan 21, 20, 00:00, 4 Years ago
Thu, Nov 7, 19, 00:00, 5 Years ago
;