Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
This versus document.forms
Message
From
11/07/2001 22:43:43
 
 
To
11/07/2001 16:38:48
General information
Forum:
Internet
Category:
Javascript
Miscellaneous
Thread ID:
00529433
Message ID:
00529547
Views:
11
>From a form action, I am calling a javascript function with the form object. However, the this syntax doesn't seem to work. I have to pass document.forms instead.
>
>This doesn't work.
>
>
>...FORM NAME=Sujet ACTION=javascript:Go(this)
>
>< SCRIPT>
>function Go(toForm)
>{
>   alert(toForm.MyField.value);
>}
>
>
>This works.
>
>
>...FORM NAME=Sujet ACTION=javascript:Go(document.forms.Sujet)
>
>< SCRIPT>
>function Go(toForm)
>{
>   alert(toForm.MyField.value);
>}
>
>
>Why?

The problem lies in the context in which Go(this) is called. I think if you use the 'javascript:Go(this)' syntax, 'this' refers to the document itself, since it is being prefixed by 'javascript'. You can test by changing your function to this:
...FORM NAME=Sujet ACTION=javascript:Go(document.forms.Sujet)

< SCRIPT>
function Go(toForm)
{
   alert(toForm.forms.sujet.MyField.value);
}
In any case, you should use the syntax as described by David Froderick, which is
...FORM NAME=Sujet ACTION='anypageevennonexistant' ONSUBMIT='return Go(this)'
In this case, 'this' refers to the form. It is perfectly legal in javascript to use the 'this' keyword to refer to an object. You can use it in any object, form field, etc. By using ONSUBMIT, you can control whether the form gets submitted or not, or do any other validation in the function, by returning true (form is submitted to ACTION value), or false (form is not submitted).

Hope this helps.
Andres M. Chiriboga, MCSE, MCP+I
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform