views:

52

answers:

2

I have HTML code which calls a javascript function from a form, using:

<form name="f" id="f" ...>
  <input name="myField" onchange="doFunct(f.myField.value,f.yourField);" />
  <input name="yourfield" onchange="doFunct(f.yourField.value,f.anotherField);" />
...

In the javascript code:

function doFunct(field,dest){
    // do something with field
    dest.value = field;
    // see if the dest has a change field
    if (dest.onchange !== null) {
        //we have an onchange function, so let's do it!
        dest.onchange();
    }
}

This works fine in Safari, Opera, and Chrome. It fails in FireFox with an error:

Error: dest.onchange is not a function

Any suggestions on how to execute "dest.onchange()" from javascript in FireFox?

I need this capability to cascade changes to fields in an input form.

A: 

To execute events, do not add the 'on' prefix to it, just run dest.change();

Lekensteyn
Actually, that's not true. That ALSO gives an error:Error: dest.change is not a function
A: 

It appears from more research, in my O'Reilly JavaScript Pocket Reference, I find there's a paragraph that states:

null (and undefined)

The JavaScript keyword null is a special value that indicates "no value". If a variable contains null, you know that it does not contain a valid value of any type. There is one other special value in JavaScript: the undefined value. This is the value returned when you use an undeclared or uninitialized variable or when you use a non-existent object property. There is no JavaScript keyword for this value.

After some testing with alert(dest.onchange) I found that Firefox was not complaining about every invocation of dest.onchange() but only those which were errors (undefined). Apparently (Grrrr!) Firefox didn't get the memo: [There is no JavaScript keyword for this value.]

If I change my code to test for dest.onchange !== undefined then Firefox is happy, but then Safari, Opera and Chrome FAIL at the test. If I change the code as below, it works in all four browsers.

if ( (dest.onchange !== null)                // test for safari, chrome, opera
     && (dest.onchange !== undefined) ) {    // test for firefox
    //we have an onchange function, so let's do it!
    dest.onchange();
}

And I got to spend 8 hours trying to figure out why Firefox doesn't play nice.