detect value change in input tag with vanilla javascript and MutationObserver

后端 未结 4 1048
一整个雨季
一整个雨季 2020-11-29 06:10

I want to detect when text/value change in input field. Even if I change the value with js, I want to detect that changes.

Here\'s what I\'ve tried so far in demo in

相关标签:
4条回答
  • 2020-11-29 06:37

    To understand what is going on is necessary to clear up the difference between attribute (content attribute) and property (IDL attribute). I won't expand on this as in SO there are already excellent answers covering the topic:

    • Properties and Attributes in HTML
    • .prop() vs .attr()
    • What is happening behind .setAttribute vs .attribute=?

    When you change the content of a input element, by typing in or by JS:

    targetNode.value="foo";
    

    the browser updates the value property but not the value attribute (which reflects the defaultValue property instead).

    Then, if we look at the spec of MutationObserver, we will see that attributes is one of the object members that can be used. So if you explicitly set the value attribute:

    targetNode.setAttribute("value", "foo");
    

    MutationObserver will notify an attribute modification. But there is nothing like properties in the list of the spec: the value property can not be observed.

    If you want to detect when an user alters the content of your input element, the input event is the most straightforward way. If you need to catch JS modifications, go for setInterval and compare the new value with the old one.

    Check this SO question to know about different alternatives and its limitations.

    0 讨论(0)
  • 2020-11-29 06:43

    I've modified Shawn's method a little and wanted to share it. Can't believe there's actually a solution to this.

    Type into the input box to see the default behavior. Now, open the DevTools and select the input element, then change its value, e.g. $0.value = "hello". Examine the UI vs. API difference. It seems UI interactions do not modify value property directly. If it were, it would also log "...changed via API...".

    let inputBox = document.querySelector("#inputBox");
    
    inputBox.addEventListener("input", function () {
        console.log("Input value changed via UI. New value: '%s'", this.value);
    });
    
    observeElement(inputBox, "value", function (oldValue, newValue) {
        console.log("Input value changed via API. Value changed from '%s' to '%s'", oldValue, newValue);
    });
    
    function observeElement(element, property, callback, delay = 0) {
        let elementPrototype = Object.getPrototypeOf(element);
        if (elementPrototype.hasOwnProperty(property)) {
            let descriptor = Object.getOwnPropertyDescriptor(elementPrototype, property);
            Object.defineProperty(element, property, {
                get: function() {
                    return descriptor.get.apply(this, arguments);
                },
                set: function () {
                    let oldValue = this[property];
                    descriptor.set.apply(this, arguments);
                    let newValue = this[property];
                    if (typeof callback == "function") {
                        setTimeout(callback.bind(this, oldValue, newValue), delay);
                    }
                    return newValue;
                }
            });
        }
    }
    <input type="text" id="inputBox" placeholder="Enter something" />

    0 讨论(0)
  • 2020-11-29 06:45

    This works and preserves and chains the original setter and getter so everything else about your field still works.

    var registered = [];
    var setDetectChangeHandler = function(field) {
      if (!registered.includes(field)) {
        var superProps = Object.getPrototypeOf(field);
        var superSet = Object.getOwnPropertyDescriptor(superProps, "value").set;
        var superGet = Object.getOwnPropertyDescriptor(superProps, "value").get;
        var newProps = {
          get: function() {
            return superGet.apply(this, arguments);
          },
          set: function (t) {
            var _this = this;
            setTimeout( function() { _this.dispatchEvent(new Event("change")); }, 50);
            return superSet.apply(this, arguments);
          }
        };
        Object.defineProperty(field, "value", newProps);
        registered.push(field);
      }
    }
    
    0 讨论(0)
  • 2020-11-29 06:50

    the value property can be observed, Don't waste your time.

    function changeValue (event, target) {
        document.querySelector("#" + target).value = new Date().getTime();
    }
     
    function changeContentValue () {
        document.querySelector("#content").value = new Date().getTime();
    }
     
    Object.defineProperty(document.querySelector("#content"), "value", {
        set:  function (t) {
            alert('#changed content value');
            var caller = arguments.callee
                ? (arguments.callee.caller ? arguments.callee.caller : arguments.callee)
                : ''
     
            console.log('this =>', this);
            console.log('event => ', event || window.event);
            console.log('caller => ', caller);
            return this.textContent = t;
        }
    });
    <form id="form" name="form" action="test.php" method="post">
            <input id="writer" type="text" name="writer" value="" placeholder="writer" /> <br />
            <textarea id="content" name="content" placeholder="content" ></textarea> <br />
            <button type="button" >Submit (no action)</button>
    </form>
    <button type="button" onClick="changeValue(this, 'content')">Change Content</button>

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