?
Solved

fireEvent('onChange') in FireFox

Posted on 2007-10-17
10
Medium Priority
?
11,217 Views
Last Modified: 2008-06-03
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?
0
Comment
Question by:Scott Bennett
  • 6
  • 3
9 Comments
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 2000 total points
ID: 20095506
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
 
LVL 14

Author Comment

by:Scott Bennett
ID: 20095870
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
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20096001
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Author Comment

by:Scott Bennett
ID: 20096514
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
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20096614
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
 
LVL 14

Author Comment

by:Scott Bennett
ID: 20096650
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
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20096923
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
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20096938
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
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20548683
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question