By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,481 Members | 1,211 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,481 IT Pros & Developers. It's quick & easy.

this.form.name -> error?

P: n/a
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

Sep 25 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

ashore wrote:
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.

Sep 25 '06 #2

P: n/a
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

Sep 25 '06 #3

P: n/a

ashore wrote:
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>

Sep 25 '06 #4

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

AS

Sep 25 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.