Skip to content Skip to sidebar Skip to footer

Detect Change, If Input-text Is Changed By Javascript

Summary- I am working on a text-comparison tool. I have two functions - an excel-parser, which fetches data from excel file and changes the input-text dynamically/programmatically

Solution 1:

You can use the MutationObserver API to do this.

Register a callback function that will deal with the changing attribute.

const input = document.getElementById('name');
const observer = newMutationObserver(list => {
  console.log("New Value: ", list[0].target.value);
})
observer.observe(input, {
  attributes: true,
  childList: false,
  subtree: false
});

functionchangeIt() {
  const randomString = Math.random() >= 0.5 ? "one" : "two";
  input.setAttribute("value", randomString);
  input.value = randomString;
}
<inputplaceholder="Enter some text"name="name"id="name" /><buttononclick='changeIt()'>Push me
</button>

You'll need to use setAttribute() and you'll need to set the Input element's value property to keep it in sync with the attribute. (as seen in the changeIt() function.

Solution 2:

You can use onkeydown for this.

var a = document.getElementById("test");

a.addEventListener('onkeydown', recInput);

functionrecInput(e) {
  console.log("Sucess!", e.key);
}

Post a Comment for "Detect Change, If Input-text Is Changed By Javascript"