How do I detect a change in the value of the INPUT?

Asked 1 years ago, Updated 1 years ago, 25 views

I would like to generate an event when changing the value of the input text element.
If you change the value in javascript, change will not generate an event.
How can it happen?

I looked it up and found that it would be good to use onpropetychange, but it doesn't work

 varobj1 = document.getElementByID("XXXX";
obj1.addEventListener(function() {"propertychange", alert("Event Occurred";}, false);

This is what I thought, but it doesn't work.

The environment is IE11, JavaScript only (no Jquery).

I look forward to hearing from you.

javascript

2022-09-30 19:24

3 Answers

As you can see in the comments, there are a lot of typographical errors, but I think it works as follows.

 varobj1 = document.getElementById("test");
obj1.addEventListener('change', function() {alert("event occurred";},false);
<input type="text" id="test"/>


2022-09-30 19:24


To ignite an event from a script I wrote in the comment that I would use fireEvent for IE, but
(IE9 seems to be a watershed)
IE11 seems to use dispatchEvent.
(createEvent, initEvent)
Example:
(I don't use IE11 myself, so I didn't test it with IE11.Sorry if it doesn't work.)

 varobj1 = document.getElementById("XXXX);
obj1.addEventListener('change', function() { alert("change event occurred";}, false);

function changeAndFire(){
	varevt = document.createEvent("Event");
	evt.initEvent("change", false, false);
	obj1.value+="Test";
	obj1.dispatchEvent(evt);
}
<input type="text" id="XXXXX"/>
<button type="button" onclick="changeAndFire()">Change values and events from scripts</button>


2022-09-30 19:24

It's been a long time, so I think it's been solved, but I encountered the same problem.
The contributor probably wanted to detect that the value was set in an external script.
If you want to set the value in your script, you can do it by processing it on the configuration side as described above.

An external script could not detect an input value in the event.
Therefore, we responded by using timer events to periodically check the input value to detect changes.
Specifically, when using the zip code-to-address conversion process, the input of the zip code field is detected and a function to convert the zip code-to-address is called, a timer is set to call a function to detect a change in the input field.
Once the state name entry field is set to a value, we did the required action, and if not, we set the timer again.


2022-09-30 19:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.