[Javascript] IE onclick problem

Howard Jess hjess at cardomain.com
Thu Apr 17 10:40:47 CDT 2008


> 
> tedd wrote:
>> At 3:56 AM +0200 4/17/08, J. Lester Novros II wrote:
>>> So I would argue that 'onsubmit="return checkMyForm()"' should read
>>> 'onsubmit="checkMyForm()"' since the handler function itself returns a
>>> true or false value to the onsubmit event, thereby cancelling or 
>>> granting
>>> the submit event.
>>
>>
>> Well -- it doesn't work that way for me.
>>
>> Here's a form where I use it:
>>
>> http://webbytedd.com/c/form-submit/
>>
>>   'onsubmit=return checkMyForm()" <-- works
>>
>> where
>>
>>   'onsubmit=checkMyForm()"  <-- doesn't
>>
>> Or is there something here I'm not understanding?
>>
>> Cheers,
>>
>> tedd
>>
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Javascript mailing list
> Javascript at lists.evolt.org
> http://lists.evolt.org/mailman/listinfo/javascript

The "onsubmit" property of the form element is defined as a function;
therefore, when you assign it a string value in markup (as you must,
since HTML attributes can only be strings, in markup), the string
is converted to a function:

   <form onsubmit="checkMyForm()"> becomes (in pseudo-javascript)
     form.onsubmit = function() {checkMyForm()}

whose value is undefined. It's much more clear to write:

   <form onsubmit="return checkMyForm()">, which becomes
     form.onsubmit = function() {return checkMyForm()}

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>

-- 
hj



More information about the Javascript mailing list