111

I have an Input in my form.

<input type="text" id="changeProgramatic" onchange="return ChangeValue(this);"/>

If I change the value in this textBox (changeProgramatic) using another JavaScript function it won't trigger the change Event.(Note: I'm passing 'this' into the method)

Subin Jacob
  • 4,692
  • 10
  • 37
  • 69
  • Possible duplicate of [How to trigger event in JavaScript?](https://stackoverflow.com/questions/2490825/how-to-trigger-event-in-javascript) – Pinke Helga Jan 14 '19 at 11:07

4 Answers4

212

Vanilla JS solution:

var el = document.getElementById('changeProgramatic');
el.value = 'New Value'
el.dispatchEvent(new Event('change'));

Note that dispatchEvent doesn't work in old IE (see: caniuse). So you should probably only use it on internal websites (not on websites having wide audience).

So as of 2019 you just might want to make sure your customers/audience don't use Windows XP (yes, some still do in 2019). You might want to use conditional comments to warn customers that you don't support old IE (pre IE 11 in this case), but note that conditional comments only work until IE9 (don't work in IE10). So you might want to use feature detection instead. E.g. you could do an early check for: typeof document.body.dispatchEvent === 'function'.

Nux
  • 9,276
  • 5
  • 59
  • 72
  • 1
    any reason why this wouldn't work in the chrome console? – ecoe Mar 19 '19 at 23:53
  • @ecoe not that I can think of... I guess there might be a problem when stepping (debugging) some other function. – Nux Mar 20 '19 at 11:01
  • 1
    Turns out I was trying to fire an event of a ReactJS input component. The React event system is an additional layer of abstraction. – ecoe Mar 20 '19 at 13:40
  • @Sunnyday hm... works fine for me for this example script: https://jsfiddle.net/eccenux/tg9o7wqy/6/ . Do you mean some custom event? Also do you mean the Edge-Edge (old, built in with Windows 10) or Edge-Edgy (Chromium based)? – Nux Jun 28 '19 at 12:05
  • 21
    Worked for me with the addition of bubbles: true... `el.dispatchEvent(new Event('change', { 'bubbles': true }));` – wkille Nov 02 '19 at 16:06
  • This should also trigger `change`, when old and new value are the same, i.e. unchanged. – Friedrich -- Слава Україні Sep 26 '22 at 22:13
43

You are using jQuery, right? Separate JavaScript from HTML.

You can use trigger or triggerHandler.

var $myInput = $('#changeProgramatic').on('change', ChangeValue);

var anotherFunction = function() {
  $myInput.val('Another value');
  $myInput.trigger('change');
};
kayz1
  • 7,260
  • 3
  • 53
  • 56
18

If someone is using react, following will be useful:

https://stackoverflow.com/a/62111884/1015678

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 }));
Lahiru Chandima
  • 22,324
  • 22
  • 103
  • 179
  • 8
    This isn't *only* useful for react. The other answers are missing the `bubbles: true`, and this has that key part. – Jeremy Jul 07 '20 at 13:20
  • "{ bubbles: true }" saved me hours of further digging, definitely worth trying for anyone having similar issues – MysticZA May 01 '23 at 08:15
10

When changing the value programmatically, you need to call the input event, not change event.

Note: The input event is fired every time the value of the element changes. This is unlike the change event, which only fires when the value is committed, such as by pressing the enter key, selecting a value from a list of options, and the like.

const el = document.getElementById('changeProgramatic');
el.value = 'New Value'
el.dispatchEvent(new Event('input', { 'bubbles': true }));
askrynnikov
  • 657
  • 10
  • 15