[Javascript] IE onclick problem

Howard Jess hjess at cardomain.com
Mon Apr 21 10:54:08 CDT 2008


 > Another question -- what if you have two forms?
 >
 > window.onload=function()
 >     {
 >     var form=document.getElementById('a');
 >     function submitter()
 >         {
 >         return checkForm(form)
 >         }
 >     form.onsubmit = submitter;
 >     var form=document.getElementById('b');
 >     function submitter()
 >         {
 >         return checkForm(form)
 >         }
 >     form.onsubmit = submitter;
 >     }
 >
 > Doesn't work for two forms.

I think it's time for you to get and read a good book on JavaScript, where most
of your questions will likely be answered; specifically, anonymous functions
(lambdas) and closures. I'd suggest Flanagan's _JavaScript_The_Definitive_Guide_.

In short,

   window.onload=function() {
     var form1 = document.getElementById('a');
     var form2 = document.getElementById('b');
     form1.onsubmit = function() {
       return checkform(form1);
     }
     form2.onsubmit = function() {
       return checkform(form2);
     }
   }

Or, as David Hucklesby implied, use DOM2 events:

   form1.addEventListener('submit',formcheck,false);
   form2.addEventListener('submit',formcheck,false);

(leaving code for IE and the formcheck function as an
exercise for the reader.)

--

hj



More information about the Javascript mailing list