Dec
7
Fire the onchange event the browser independent way
Filed Under ColdFusion, HTML, JavaScript | 30 Comments
I have faced a whole of lot of issues trying to fire the html events, mostly the onchange event, from JavaScript. The situation was pretty simple. I modified the value of a text box from JavaScript and I now wanted to fire the onchange event programmatically. Tried a lot of solutions suggested. When I finally thought I had a solution that worked, the solution worked on one browser but failed to work on the other.
Now that I have figured out the way of getting this to work, thought of sharing it. It will surely help someone out there who is trying to look for a solution that works on all browsers. By the way, I have tested this on IE, FireFox and Safari and it seems to work fine.
On IE, firing an event is as simple as calling the fireEvent method on the html element by passing the name of the event to it.
Element.fireEvent(eventName)
txt.fireEvent(’onChange’);
On Gecko based browsers, there’s more work involved. First of all we need to create the event, initialize it and then finally dispatch it.
Step 1: Create the event
var evt = document.createEvent(’HTMLEvents’);
Step 2: Call initEvent on the event object
evt.initEvent(’change’, true, true);
The first parameter here is the name of the event. The second one indicates whether the event can bubble or not. And the final one specifies whether or not the event’s default action can be prevented or not.
Step 3: Dispatch the event
txt.dispatchEvent(evt);
txt here is the html element.
Here’s the complete html code that does just this.
<html>
<head>
<script language=”javascript”>
function setText()
{
var txt = document.getElementById(’testinput’)
txt.value=”value”
//On IE
if(txt.fireEvent)
{
txt.fireEvent(’onchange’);
}
//On Gecko based browsers
if(document.createEvent)
{
var evt = document.createEvent(’HTMLEvents’);
if(evt.initEvent)
{
evt.initEvent(’change’, true, true);
}
if(txt.dispatchEvent)
{
txt.dispatchEvent(evt);
}
}
}
function textChangeHandler()
{
alert(’value changed’);
}
</script>
</head>
<body>
<input type=”text” id=”testinput” onchange=”textChangeHandler();”>
<input type=”button” value=”set text” onclick=”setText();” >
</body>
</html>



I use jQuery for nearly all my js needs, and it has cross browser event handling and triggering baked in.
Right Sam. You do not need to worry about firing events if you are working with frameworks such as jQuery.
This just saved me several hours of work and I really appreciate it. Merry Christmas and thanks.
Thanks Tom. I posted it for the very same reason. – Save others the hours of effort I put in to get this working.
Merry Christmas to you as well.
saved my day. thanx
Clean & neat. Thanks. This is quite close to what I was looking for
Thanks,
You also save me several hours of desperation.
You saved at least half of my working day. Thanks! Best wishes.
Thanks for the tutorial. And thank you IE and firefox for taking 2 completely different approaches to this.
Awesome tip, thank you! I fought this battle some months ago and lost, but with this tip I was able to replace a bunch of crappy work around code in no time flat. Much appreciated!
Just, Thanks
Hi,
I have a similar issue..
I am using a javascript calendar. and this calendar fills the data into a text box.
Now, what i have to do is… When ever the date is changed in the text box, a javascript function has to be called. I cannot change the calendar script as this is used in may places in the application.
Can you please suggest me a solution for this??
This text box is readonly
Thanks in advance….
@ R P & Mohamed – Am glad it helped.
@ Praveen – You say you cannot change the cakebdar script. Does your calendar fire an event when a date is selcted from the calendar? If that is the case you can register for the date selected event and fire the onchange event for the text box in that event handler. Hope it helps.
Thanks a lot… i was able to do it in this way..
Muchas Gracias!
Great article! It’s much more simple then onpropertychange, onchange, onkeyup approach and browser detect. Thanks again!
I am having issues in firing an event with the Chrome browser. Do you know why?
Thanks a lot,
Hemanth
Yes, another saved day. I’m using YUI and I can’t believe they don’t have this cross-browser impl wrapped in their lib. Anyhow, much appreciated!!
I can sleep tonight thanks to your code
@Hemanth: hmm.. Strange.. I have never tried to get it working on chrome.
Thank you a lot! You saved my day.
I am using a javascript calendar. and this calendar fills the data into a text box.
Now, what i have to do is… When ever the date is changed in the text box, a javascript function has to be called. I cannot change the calendar script as this is used in may places in the application.
Can you please suggest me a solution for this??
This text box is readonly .
My calendar does not fire any event when a date is selcted from the calendar. How to do it?
Good stuff! I’ve been googling for a while trying to find this exact solution. Thanks.
@ Hemangini: I am not very sure how you would do this without the support from the javascript calendar that you are using. The calendar should ideally expose an event to which you can attach you javascript function.
Thanks HEAPS for that post, man!
OnChange was doin my head in….. I was using addEventListener to attach the event and it doesn’t simply fire by calling the onchange() function.
However, your code here worked!!
Thanks heaps, man!
BIG THANK YOU
I am looking for this exact same thing, I have tried to follow your code and added the following to my app:
//Fire event //On IE
if(input_field.fireEvent)
{
input_field.fireEvent(’onChange’);
}
//Fire event On Gecko based browsers
if(document.createEvent)
{
var evt = document.createEvent(’HTMLEvents’);
if(evt.initEvent) { evt.initEvent(’change’,true, true); }
if(input_field.dispatchEvent)
{
input_field.dispatchEvent(evt);
}
}
It does not work on Firefox and Chrome, I have not test on other browsers. I use jQuery as the base framework for my app, how can i do this easier perhaps with jQuery??
Thanks a ton!!
Thank you very much, it works
@theUnseen – If you are using jQuery, i guess you do not have to worry about handling this as jQuery should take care of browser differences.