First off, I think Twey is right on the technicality he mentions. It is the proper way but, most modern browsers will allow the shorter method.
Next, you are correct about the shorthand used in the scripting technique. The economy of code rests upon the 'this' token. When an element has an event and that event is configured for it in any way other than the attachEvent method (an IE proprietary method), then the 'this' keyword refers to the element itself. In both my example:
Code:
<form action="#" onsubmit="check_val(this);">
Which to be more like what you have could be written like so:
Code:
<form action="#" onsubmit="return check_val(this);">
(using it like that simply is a way of guaranteeing that the return value of the function is passed in the handler back to the event itself)
and in your example:
Code:
<input type="submit" value="Send" onClick="return check_val(this.form);" />
The this keyword is being used to pass the form itself to the function. In your case, since the event is triggered by a button in the form, it becomes 'this.form' - in mine, since it is triggered by the form tag itself, it is simply 'this'.
The other reason it works is that a function written in this format:
Code:
function functionName(parameter) {
alert(parameter);
}
gets the value of parameter from the way it was called, it doesn't need to be formally set:
Code:
functionName('Hello World');
will pop up an alert with Hello World on it.
One other 'trick' used in the validation function you have is that the field being tested is assumed to have the name of 'name':
then refers to the value of an element of the form in question that has the name of 'name':
HTML Code:
<input type="text" name="name">
Bookmarks