I\'ve got an input
with google autoComplete
connected.
When the user moves up and down through the seachResults
the value of t
The .value
attribute will only change if the script-writer has called setAttribute('value'
to set the new value, which is quite an odd thing to do. In almost all situations, I would expect the value to be set by assigning to the value
property directly, eg:
input.value = 'foo';
Calling setAttribute
will indeed show a change in the inspected DOM attribute, eg:
<input value="somevalue">
const input = document.querySelector('input');
input.setAttribute('value', 'foo');
console.log(input.outerHTML);
<input>
But just assigning to the .value
property of the element will not result in such a change:
const input = document.querySelector('input');
input.value = 'foo';
console.log(input.outerHTML);
<input>
Assigning to the .value
actually invokes a setter function on HTMLInputElement.prototype
:
console.log(HTMLInputElement.prototype.hasOwnProperty('value'));
You can shadow this by putting a getter/setter for value
directly on the element itself, with the setter invoking your own function, allowing you to listen for changes:
const { get, set } = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value');
const input = document.querySelector('input');
Object.defineProperty(input, 'value', {
get() {
return get.call(this);
},
set(newVal) {
console.log('New value assigned to input: ' + newVal);
return set.call(this, newVal);
}
});
// example of autocomplete trying to change the input value:
input.value = 'foo';
<input>
Consider creating and triggering input events
var event = new Event('input', {
bubbles: true,
cancelable: true,
});
then
myelement.dispatchEvent(event);
more info