Link to home
Start Free TrialLog in
Avatar of Scott Bennett
Scott BennettFlag for United States of America

asked on

fireEvent('onChange') in FireFox

I have some code that programatically fires an onchange event like:

document.Myform.MyTextField.fireEvent('onChange');

works fine in IE but not in FireFox. Can anyone tell me how to make it work in FireFox?
ASKER CERTIFIED SOLUTION
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Scott Bennett

ASKER

This sample from the second link looks kind of like what I need:
function fireClickEvent(control)
{
    if (document.all)
    {
        control.fireEvent("onclick");
    }
    else
    {
        var clickEvent = window.document.createEvent("MouseEvent");
        clickEvent.initEvent("click", false, true);
        control.dispatchEvent(clickEvent);
    }
}

but I don't know how to convert it for use with onChange which is not a "MouseEvent"

I have done this:
<script language="javascript" type="text/javascript" for="window" event="onload">
    if (document.all) { initialize(); }
    else { window.document.addEventListener("DOMContentLoaded", initialize, true); }
</script>
<script language="JavaScript">
function fireChangeEvent(control)
{
    if (document.all)
    {
        control.fireEvent("onchange");
    }
    else
    {
        var changeEvent = window.document.createEvent("HTMLEvent");
        changeEvent.initEvent("change", false, true);
        control.dispatchEvent(changeEvent);
    }
}
</script>
but it throws errors, any thoughts?
My first thought is to use change instead of HTMLEvent.  For example ...

    else
    {
        var changeEvent = window.document.createEvent("change");
        changeEvent.initEvent("change", false, true);
        control.dispatchEvent(changeEvent);
    }

Let me know how that works.  This might be something that browser security will prevent (IE has never been the most careful regarding that :-] ).  Scripting an onchange event "firing" would be something a browser might be cautious of and prevent.  If that is the case then this won't work.  I am not saying we can't because I haven't read anything (yet) that said it but just a caution.

bol
That doesn't work either it is the first line of:
<script language="javascript" type="text/javascript" for="window" event="onload">
    if (document.all) { initialize(); }
    else { window.document.addEventListener("DOMContentLoaded", initialize, true); }
</script>
that is causing the error
What are you trying to do with that script tag?  The for and event "attributes" aren't part of the W3 specs to my knowledge.

Also, what is the event listener you are trying to add?

bol
I have no Idea I just copied it from that page on the second link you posted:
http://blogs.vertigo.com/personal/aanttila/Blog/Lists/Posts/Post.aspx?ID=6

should I take that part out?
Yes remove that for what we are working on here.  That script is suggested for something else.  To be honest I am not sure what or how useful it would be.  It should not be required for what we are trying.

bol
One thing I can tell you for sure is that script seems to be used when you want to run an "initialize" function when the page loads.  We aren't worried about the page load and you probably don't even have a function with that name.

bol
Thanks for coming back to close this.  I'm glad that I could help.  Thanks for the grade, the points and the fun question.

bol