161

I installed an event handler on an input using

var element = document.getElementById('some-input');
element.addEventListener('input', function() {
    console.log('The value is now ' + element.value);
});

As expected, the handler is triggered when I type into the text field, but I also need to invoke this handler from my code. How can I simulate the input event so that my event listener is called?

4
  • possible duplicate of stackoverflow.com/questions/2381572/… Commented Feb 26, 2016 at 18:52
  • 1
    @user2473779 The answer there implies that I should do element.input(), but that’s not a valid method. Commented Feb 26, 2016 at 18:56
  • jsfiddle.net/mendesjuan/rHMCy/4 : check this fiddle from @Juan Mendes' answer Commented Feb 26, 2016 at 19:01
  • 1
    @user2473779 The function given there doesn’t handle “input” events. See this modified version of the Fiddle, which uses an input element instead of an a element. Editing the text triggers the event listener but clicking the button just produces an error. Commented Feb 26, 2016 at 19:19

3 Answers 3

267

Create an Event object, and dispatch it:

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

Or, as a one-liner:

element.dispatchEvent(new Event('input', { bubbles: true }));

FIDDLE

This is not supported in IE, for that the old-fashioned way has to be used:

var event = document.createEvent('Event');
event.initEvent('input', true, false);

element.dispatchEvent(event);
Sign up to request clarification or add additional context in comments.

6 Comments

@jeff-h From MDN: Older versions of IE supported an equivalent, proprietary EventTarget.fireEvent() method. developer.mozilla.org/en-US/docs/Web/API/EventTarget/…
A note: the default input event is not cancelable
modern copy paste: element.dispatchEvent(new Event('input', { bubbles: true }))
How is this difference from new InputEvent() and when should InputEvent be used instead?
InputEvent is a newer Spec by MDN and not yet HTML standard, albeit widely supported. If you don't need it, I would simply use "Event" for it's been here forever and won't change much anymore. github.com/microsoft/TypeScript/issues/…
|
41
element.dispatchEvent(new Event('input'));

Comments

26

If you are using react, following will work:

const valueSetter = Object.getOwnPropertyDescriptor(this.textInputRef, 'value').set;
const prototype = Object.getPrototypeOf(this.textInputRef);
const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
if (valueSetter && valueSetter !== prototypeValueSetter) {
    prototypeValueSetter.call(this.textInputRef, 'new value');
} else {
    valueSetter.call(this.textInputRef, 'new value');
}
this.textInputRef.dispatchEvent(new Event('input', { bubbles: true }));

3 Comments

Object.getOwnPropertyDescriptor(this.textInputRef, 'value') -> returns undefined, sometimes.
YES - thank you. Was pulling my hair out on this when I realized the client site was a react app. Worked great when no other change or keypress methods were working and the textarea would just revert to blank upon losing focus. This fixed it and triggers the change event as needed.

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.