21

As you can see in the code section below, once you click on one the buttons the text in the field changes but the event does not get fired (and it does not run alertTextInput() method), when you type text directly under the text field the event is triggered as expected.

How can I activate the event manually by using a code or using a method that changes the text and triggers the event? thank you!

const changeTextButton = document.querySelector("#change-text");
const clearButton = document.querySelector("#clear");
const inputField = document.querySelector("#query");

changeTextButton.addEventListener("click", changeText);
clearButton.addEventListener("click", clear);
inputField.addEventListener("input", alertTextInput);

function changeText() {
  inputField.value = "Demo Text!!";
  inputField.dispatchEvent(new Event("input"))
}

function clear() {
  inputField.value = "";
  inputField.dispatchEvent(new Event("input"))
}

function alertTextInput() {
  alert(inputField.value);
}
<input type=text id="query">
<button id="change-text">Change the text programmatically</button>
<button id="clear">Clear</button>

6
  • 1
    what text you wanna change ? i am having a hard time understunding your question Commented Mar 19, 2018 at 17:54
  • I'm assuming you're asking how to get the alertTextInput() to run after you change the text through inputField.value = "Demo Text!!"; - unfortunately that isn't considered an event for the input event listener. You'd have to call that manually I believe. Commented Mar 19, 2018 at 17:55
  • possible duplicate: stackoverflow.com/questions/16013024/… Commented Mar 19, 2018 at 17:57
  • This is another duplicate - stackoverflow.com/questions/42427606/… Commented Mar 19, 2018 at 17:58
  • You are welcome to see the answer below, from my impression The question is not duplicated from the links attached above. Commented Mar 19, 2018 at 18:22

4 Answers 4

41

Try this to manually dispatch the event, see EventTarget.dispatchEvent():

inputField.dispatchEvent(new Event("input"))
Sign up to request clarification or add additional context in comments.

1 Comment

This however won't set the inputType property correctly (when checking event.inputType), see this for reference: Mozilla.org and W3C
15

From React Doc

React expects to hear the input event

So trigger the input event after set the value. This is how to trigger.

The Final code is:

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

inputElement.value = '0.2'
inputElement.dispatchEvent(event);

1 Comment

inputElement.value would normally have to be taken from the target though. Something like document.getElementById("query").value in OP's example. Actually, I see they save that element in the inputField const so event.target = inputField would be simpler.
2

I find it important to notice, that the input event has some complexity around the event.inputType property which can be challenging to mimic.

The inputType event property is used to determine the kind of change that was performed, since the input event is triggered after the change has taken place.

Notice, it is not allowed to set the inputType as an event detail on a system event, but this work-around seems to work in my own tests.

    const inpType = "insertText";

    const evt = new Event('input', { bubbles: true, cancelable: true })
    evt.inputType = inpType;
    inputField.dispatchEvent(evt);

Please read this documentation (and try the sample within) on Mozilla.org

Here are the behaviors triggering the various inputType enums: w3c.org

Comments

1

If I understand correctly you want the input text to change AND the alert to appear when clicking "change" button. As the changeText() function just changes the text you should not expect an alert to appear.

You could just add the alertTextInput() function to the bottom of changeText().

function changeText() {
  inputField.value = "Demo Text!!";
  alertTextInput();
}

2 Comments

I prefer an event-based solution
I had a similar situation as the OP and found this answer was useful for me. I tried the dispatchEvent("input) paired with addEventListener("input", myfunc) but it did not work for some reason. I like your simple solution. So thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.