To understand what is going on, it is necessary to clarify the difference between an attribute (content attribute) and a property (IDL attribute). I will not expand on this since there are already great answers in SO covering the topic:
- Properties and Attributes in HTML
- .prop () vs. attr ()
- What happens for .setAttribute vs.attribute =?
When you change the contents of an input element by typing or using JS:
targetNode.value="foo";
the browser updates the value property but not the value attribute (which instead reflects the defaultValue property).
Then, if we look at the MutationObserver specification , we will see that attributes are one of the members of the object that can be used. Therefore, if you explicitly value attribute:
targetNode.setAttribute("value", "foo");
MutationObserver will notify you of an attribute change. But there is nothing like properties in the specification list: the value property cannot be detected .
If you want to determine when the user changes the contents of your input element, the input event is the easiest way. If you need to catch JS modifications go to setInterval and compare the new value with the old one.
Check this question to learn about the various alternatives and its limitations.
David
source share