616

I'm setting a date-time textfield value via a calendar widget. Obviously, the calendar widget does something like this :

document.getElementById('datetimetext').value = date_value; 

What I want is:
On changing value in the date-time textfield I need to reset some other fields in the page. I've added a onchange event listener to the datetimetext field which is not getting triggered, because I guess onchange gets triggered only when the element gets focus & its value is changed on losing focus.

Hence I'm looking for a way to manually trigger this onchange event (which I guess should take care of checking the value difference in the text field).

Any ideas?

4
  • 14
    $(document.activeElement).trigger("change"); Commented Mar 18, 2016 at 18:45
  • 2
    $(<ELEMENT_SELECTOR>).change(); Commented Jun 15, 2018 at 16:31
  • 3
    the second requires jquery Commented May 22, 2019 at 16:54
  • also see mozilla: creating and triggering events Commented Sep 17, 2024 at 12:51

2 Answers 2

697

MDN suggests that there's a much cleaner way of doing this in modern browsers:

// Assuming we're listening for e.g. a 'change' event on `element` // Create a new 'change' event var event = new Event('change'); // Dispatch it. element.dispatchEvent(event); 
Sign up to request clarification or add additional context in comments.

19 Comments

@BrettZamir Worked for me (at least in Chrome 36) the following way: document.querySelector('select.freight').dispatchEvent(new Event('change', { 'bubbles': true }))
Just FYI on this... MDN does mention this, but its not supported by IE10+ (arguably also modern by definition of the word modern..) and only in the nightly of Safari (as time of writing) IE10+ does have this constructor method but you need to specify the type of event, eg. MouseEvent or KeyboardEvent :see:- msdn.microsoft.com/en-us/library/ie/dn905219%28v=vs.85%29.aspx
This does not work on IE11
element.dispatchEvent(new window.Event('change', { bubbles: true })) if you want to bubble too
It's new CustomEvent('change') now(June 2020/Covid-19).
|
593

There's a couple of ways you can do this. If the onchange listener is a function set via the element.onchange property and you're not bothered about the event object or bubbling/propagation, the easiest method is to just call that function:

element.onchange(); 

If you need it to simulate the real event in full, or if you set the event via the HTML attribute or addEventListener/attachEvent, you need to do a bit of feature detection to correctly fire the event:

if ("createEvent" in document) { var evt = document.createEvent("HTMLEvents"); evt.initEvent("change", false, true); element.dispatchEvent(evt); } else element.fireEvent("onchange"); 

13 Comments

thanks. this works, but am not sure about browser detection here. wondering if there is a way to do the same via YUI library. thanks anyways.
Thanks. Seems to be working in Android's WebView with those 3 lines from else block.
Andy E, this does not work in IE10, any ideas?
@NickBinnet: interesting. I'm working in Linux at the moment and can't switch to test for a solution, but I would suggest swapping the if and else blocks, and testing for if ('createEvent' in document) instead. Let me know if this works and I'll update the answer.
This is "The old-fashioned way". Now there is another way. See Milan answer.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.