this.form.name -> error?

A

ashore

Folks, the snippet below errors out on both FF and IE, with FF's error
console complaining "this.form has no properties", while a reference to
"document.forms[0].name" works correctly.

<form name='whatever1' method = "post"
action="javascript:alert(this.form.name);">
<input type="button" value="click" onClick = "this.form.submit();"/>
</form>

Any ideas welcome,

AS
 
W

web.dev

ashore said:
Folks, the snippet below errors out on both FF and IE, with FF's error
console complaining "this.form has no properties", while a reference to
"document.forms[0].name" works correctly.

<form name='whatever1' method = "post"
action="javascript:alert(this.form.name);">
From that context it would not work, since the form element does not
have a form. Therefore, simply reducing it down to this.name should
suffice.

The javascript pseudo-protocol is highly discouraged, and you can find
the reasons why by searching this newsgroup.
<input type="button" value="click" onClick = "this.form.submit();"/>

If you're just going to submit a form when clicking on this button,
it's easier to just use a submit type.

<form name = "whatever1" action = "file.ext" onsubmit =
"alert(this.name)">
<input type = "submit" value = "click">
</form>

Remember to do the appropriate handling for submit, as it stands it
will still submit the form.
 
A

ashore

web.dev, thanks, but please explain yr comment " ...since the form
element does not have a form ..."

FYI, I tried "this.name", with an empty result.

Thanks again,

AS
 
W

web.dev

ashore said:
web.dev, thanks, but please explain yr comment " ...since the form
element does not have a form ..."

When you use this.form, it refers to the parent form. From your
context, your form does not have a parent form, plus it is also invalid
markup to have a form within a form. Therefore, this.form does not
make sense from where you are using it.
FYI, I tried "this.name", with an empty result.

In the example that I've tried comes up with the result you're looking
for:

<html>
<head>
<title></title>
</head>
<body>
<form name = "whatever1" action = "file.ext" onsubmit =
"alert(this.name)">
<input type = "submit" value = "click">
</form>
</body>
</html>
 
L

Lee

ashore said:
Folks, the snippet below errors out on both FF and IE, with FF's error
console complaining "this.form has no properties", while a reference to
"document.forms[0].name" works correctly.

<form name='whatever1' method = "post"
action="javascript:alert(this.form.name);">
<input type="button" value="click" onClick = "this.form.submit();"/>
</form>

Any ideas welcome,

Your mistake seems to be using the "javascript:" pseudo-protocol
without completely understanding how it works. Don't do that.

The statement "alert(this.form.name)" executes in the context of
the window, not the form, and thw window (this) doesn't have an
attribute named "form".


--
 
A

ashore

Big-time thanks! I'd been using the this.form construct for years, but
always in the context of a form element.

AS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top