How do you listen / detect changes to an input value - when the input value is changed via javascript?

后端 未结 2 2007
失恋的感觉
失恋的感觉 2020-12-21 10:29

I\'ve got an input with google autoComplete connected.

When the user moves up and down through the seachResultsthe value of t

相关标签:
2条回答
  • 2020-12-21 11:00

    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>

    0 讨论(0)
  • 2020-12-21 11:07

    Consider creating and triggering input events

    var event = new Event('input', {
        bubbles: true,
        cancelable: true,
    });
    

    then

    myelement.dispatchEvent(event);
    

    more info

    0 讨论(0)
提交回复
热议问题