473,324 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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 1531
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mikko | last post by:
Is it possible to read "label" from form-field? <label for="product1">bread</label> <input id="product1" name="product1" type="text" size="3"> number comes with $_GET; but how do I read the...
3
by: Pavils Jurjans | last post by:
Hello, I have bumped upon this problem: I do some client-side form processing with JavaScript, and for this I loop over all the forms in the document. In order to identify them, I read their...
2
by: /.. | last post by:
Hi all, I'm working on a report display page that currently uses 5 checkboxlists with a total of 86 items to display values from 5 different tables in an Access database. The page works fine...
12
by: James Norton-Jones | last post by:
Hi, Am I trying to hold the data of a DataGrid in a label so that when the form is reposted the DataGrid can be repopulated. The problem I am having is that I don't understand how to get the...
17
by: ronaldlee | last post by:
I have this error in Line 89. Collection is read-only. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more...
2
by: Vish | last post by:
Hi, I amplanning on having a rea-only and edit states for my form. But it do not want my form and its controls to look different or disabled. I am planning on having a edit button that brings...
0
by: vishnu | last post by:
Hi, Am trying to post the data over https and am getting error in httpwebresponse.getResponseStream.Please help me to get rid of this issue. Here is the message from immediate window ...
4
by: Michael Munch | last post by:
Hi I want to read the value of af text-field, create dynamic, in a form. Se below a small test-site to do that (but readning fails): I use the function Test_Read for reading the value from the...
11
beacon
by: beacon | last post by:
Hi everybody, I created a database that links one table from an ODBC data source. I saved my password and UID to the data source so neither myself nor anyone else would have to login each time...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.