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?
LVL 14
Scott BennettManager TechnologyAsked:
Who is Participating?
 
b0lsc0ttConnect With a Mentor IT ManagerCommented:
SBennett,

The fireEvent method is only for IE.  There are alternatives.  I don't have one that I wrote myself but take a look at these.

http://www.howtocreate.co.uk/tutorials/javascript/domevents

OR

http://blogs.vertigo.com/personal/aanttila/Blog/Lists/Posts/Post.aspx?ID=6

Let me know how those will work.  I'll be happy to help more if you need or have a question.  They both look good (scroll down a bit on each page to find the "manually fire event" or "firing events" section).  Let me know if you have any questions or need more information.

b0lsc0tt
0
 
Scott BennettManager TechnologyAuthor Commented:
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?
0
 
b0lsc0ttIT ManagerCommented:
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
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Scott BennettManager TechnologyAuthor Commented:
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
0
 
b0lsc0ttIT ManagerCommented:
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
0
 
Scott BennettManager TechnologyAuthor Commented:
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?
0
 
b0lsc0ttIT ManagerCommented:
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
0
 
b0lsc0ttIT ManagerCommented:
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
0
 
b0lsc0ttIT ManagerCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.