[Javascript] IE onclick problem

Howard Jess hjess at cardomain.com
Fri Apr 18 13:57:42 CDT 2008



tedd wrote:
 > At 8:40 AM -0700 4/17/08, Howard Jess wrote:
 >> And finally, it's *far* better to leave this attribute out of
 >> markup altogether, and do everything in javascript:
 >>
 >>     <form id="someform"> .... </form>
 >>
 >>     <script type="text/javascript">
 >>     window.onload=function() {
 >>       var form=document.getElementById('someform');
 >>       if (form) form.onsubmit = checkMyForm;
 >>     }
 >>     </script>
 >
 > Howard:
 >
 > I spoke too soon -- take a look at this:
 >
 > http://webbytedd.com/ccc/test-onsubmit1/index.php
 >
 > Please note that while not entering the data required in each text box
 > will generate an alert, it will not stop the submit.
 >
 > I provided a demo here:
 >
 > http://webbytedd.com/ccc/test-onsubmit/index.php

The onsubmit property is a function reference, as I suggested:

   form.onsubmit = checkMyForm;

Your example, which doesn't work, says:

window.onload=function() {
   var form=document.getElementById('a');
   if (form) {
     form.onsubmit = checkForm(form);
   }
}

The line you specify invokes the checkForm function, rather than
referring to it, and sets the onsubmit property to false; notice
that you get the alert prompt on first display of the page, *before*
you hit submit.

Try:
   window.onload = function() {
     var form = document.getElementById('a');
     form.onsubmit = function() {return checkForm(form)};
   }

or if you don't like that, try:
   window.onload = function() {
     var form = document.getElementById('a');
     function submitter() {
       return checkForm(form);
     }
     form.onsubmit = submitter;
   }


-- 
Howard Jess | CarDomain Network
Software Engineer
1633 Westlake Avenue North, Suite 100, Seattle, WA  98109
hjess at cardomain.com | tel 206.926.2144 | fax 206.926.2299
http://members.cardomain.com/verklempt



More information about the Javascript mailing list