[Javascript] Targeting the correct form??
Paul Novitski
paul at juniperwebcraft.com
Wed Feb 25 13:40:25 CST 2009
At 2/25/2009 11:07 AM, Terry Riegel wrote:
>This one worked
>----------------
>function forms(h) {
> var daItems = $$('.'+h);
> for (iItem=0; iItem < daItems.length; iItem++) {
> var o=daItems[iItem]
> o.onsubmit=doforms;
> }
>}
>
> var doforms = function() {
> alert(this.id);
> return false;
> }
>
>
>This one did not work
>---------------------
>function forms(h) {
> var daItems = $$('.'+h);
> for (iItem=0; iItem < daItems.length; iItem++) {
> var o=daItems[iItem]
> o.onsubmit=function(){return doforms(this)};
> }
>}
>
>function doforms() {
> alert(this);
> return false;
> }
Terry,
An important question to ask yourself when considering the code is:
what does 'this' (or any function argument) represent when it's
applied to a behavior trigger, and what does it represent when the
function is later called?
When a function is called as the result of an event, 'this' refers to
the object whose event has just occurred. In your second example
above, the one that did not work, in function forms(h){} I'm not
really sure what, if anything, 'this' refers to, but it's certainly
not some specific form being triggered sometime in the future. When
you're creating a function on the fly, its arguments take their
current value (in the moment that the function is being created) and
not their hypothetical value sometime later when the function is called.
It seems to me that the main reason your second example doesn't work
is that you're calling "alert(this);" instead of "alert(this.id);" as
you do in your first example. Trying to display an object with
alert() won't get you much valuable information, but displaying an Id
might. Passing 'this' to the doforms() function in your second
example is just a red herring and doesn't accomplish anything that I
can see. It certainly isn't affecting the meaning of 'this' inside doforms().
Clear as mud?
By the way, I'm not sure why you're using the variable 'o' to point
to the form object for a few milli- or microseconds:
var o=daItems[iItem]
o.onsubmit=doforms;
You could simply write:
daItems[iItem].onsubmit=doforms;
since daItems[iItem] is a perfectly legal representation for the form
object. The only reason I would use a temporary variable as you're
doing would be if I needed to test it to make sure it's really there,
you know, like:
var o=daItems[iItem]
if (!o) continue;
o.onsubmit=doforms;
But your code has PRESUMABLY already guaranteed that each item in
your array is indeed a form object.
Of course, your code doesn't make it clear HOW you know that daItems
are actually forms. Like David, I really dislike the $ syntax. My
attitude is that scripting languages are for people, not
computers. If your code isn't easy for you to read today, or you to
read a year from now, or for someone else to read next week when you
get hit by the proverbial sudden need to escape to Tahiti, you might
as well just write in machine language which will be just as
human-unreadable but at least will execute significantly faster.
Regards,
Paul
__________________________
Paul Novitski
Juniper Webcraft Ltd.
http://juniperwebcraft.com
More information about the Javascript
mailing list