469,336 Members | 5,570 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

How to read Form name?

Hello,

I have a javascript function that I want to read the name of the form
which is calling it.

Here is my HTML:

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function formNameTest(oThis)
{
alert('Form name is "'+oThis.form.name+'"')
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="Test">
<INPUT TYPE="TEXT" NAME="TestText" onClick="formNameTest(this)">
</FORM>
</BODY>
</HTML>

and what it ends up displaying in the alert window is "[object]" instead of
the form's name. I have also tried using 'oThis.form.name.value' which
gives "".
And finally, I tried passing 'this.form' to my fuction (and modified my
function) instead of 'this', with no luck.

So, how can this be done?

Thanks in advance,
Scott Navarre
Jul 20 '05 #1
3 1387
<html><body>

<script language="JavaScript">
function formName(theFormName){
alert(theFormName);
}
</script>
<form name="myForm">
<input type="text" name="theButton"
onClick="formName(this.form.name);">
</form>

</body></html>

Brynn
www.coolpier.com

I participate in the group to help give examples of code.
I do not guarantee the effects of any code posted.
Test all code before use!

On Wed, 21 Jan 2004 01:39:32 -0700, "Scott Navarre" <sm*@asus.net>
wrote:
Hello,

I have a javascript function that I want to read the name of the form
which is calling it.

Here is my HTML:

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function formNameTest(oThis)
{
alert('Form name is "'+oThis.form.name+'"')
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="Test">
<INPUT TYPE="TEXT" NAME="TestText" onClick="formNameTest(this)">
</FORM>
</BODY>
</HTML>

and what it ends up displaying in the alert window is "[object]" instead of
the form's name. I have also tried using 'oThis.form.name.value' which
gives "".
And finally, I tried passing 'this.form' to my fuction (and modified my
function) instead of 'this', with no luck.

So, how can this be done?

Thanks in advance,
Scott Navarre


Brynn
www.coolpier.com

I participate in the group to help give examples of code.
I do not guarantee the effects of any code posted.
Test all code before use!
Jul 20 '05 #2
"Scott Navarre" <sm*@asus.net> wrote in message
news:bu*********@enews4.newsguy.com...
<snip>
function formNameTest(oThis)
{
alert('Form name is "'+oThis.form.name+'"')
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="Test">
<INPUT TYPE="TEXT" NAME="TestText" onClick="formNameTest(this)">
</FORM>
</BODY>
</HTML>

and what it ends up displaying in the alert window is
"[object]" instead of the form's name. I have also tried
using 'oThis.form.name.value' which gives "".
And finally, I tried passing 'this.form' to my fuction
(and modified my function) instead of 'this', with no luck.


When you post code claming that it demonstrates a problem it is a good
idea to test it to ensure that id does exhibit the problem. The code you
posted will not exhibit the problem described because that is being
caused by one of the controls that you have placed within the real form
HTML having the name (or possibly ID) "name". When the reference to that
control is added to the form as a named property of the form, under its
name "name" it overwrites the form's original - name - property and
renders the name of the form inaccessible to scripts.

If you had tested the code you posted you would have noticed it working
and had reason to suspect that it was a difference between that code and
the original that explained the problem.

It is best to never give the controls within a form NAME (or ID)
attributes that correspond with existing properties of the form object.
And it is easy to avoid as JavaScript is case sensitive so only changing
to initial capitals in the control names would avoid the problem
entirely.

Richard.
Jul 20 '05 #3
Yes, you are right! I had a text input with NAME="name"!

I didn't know it would override the form's .name attribute. Thank you
very much for explaining this! And for knowing exactly what I did without
seeing my original code. You're good...

So then you would suggest to start all HTML form element names with
capital letters to never run into a problem like this again?
I guess I am just use to JavaScript's variable naming convention of always
using lowercase letters for the first letter of the variable.
I am not sure I would have realized the problem even if I knew my example
worked...
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Mikko | last post: by
2 posts views Thread by /.. | last post: by
12 posts views Thread by James Norton-Jones | last post: by
17 posts views Thread by ronaldlee | last post: by
2 posts views Thread by Vish | last post: by
4 posts views Thread by Michael Munch | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.