How to read Form name?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott Navarre

    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="JavaS cript">
    function formNameTest(oT his)
    {
    alert('Form name is "'+oThis.form.n ame+'"')
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM NAME="Test">
    <INPUT TYPE="TEXT" NAME="TestText" onClick="formNa meTest(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.nam e.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

    #2
    Re: How to read Form name?

    <html><body>

    <script language="JavaS cript">
    function formName(theFor mName){
    alert(theFormNa me);
    }
    </script>


    <form name="myForm">
    <input type="text" name="theButton "
    onClick="formNa me(this.form.na me);">
    </form>

    </body></html>

    Brynn


    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" <smn@asus.net >
    wrote:
    [color=blue]
    >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="JavaS cript">
    > function formNameTest(oT his)
    > {
    > alert('Form name is "'+oThis.form.n ame+'"')
    > }
    > </SCRIPT>
    > </HEAD>
    ><BODY>
    > <FORM NAME="Test">
    > <INPUT TYPE="TEXT" NAME="TestText" onClick="formNa meTest(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.nam e.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
    >
    >[/color]

    Brynn


    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!

    Comment

    • Richard Cornford

      #3
      Re: How to read Form name?

      "Scott Navarre" <smn@asus.net > wrote in message
      news:bule1a0193 9@enews4.newsgu y.com...
      <snip>[color=blue]
      > function formNameTest(oT his)
      > {
      > alert('Form name is "'+oThis.form.n ame+'"')
      > }
      > </SCRIPT>
      > </HEAD>
      ><BODY>
      > <FORM NAME="Test">
      > <INPUT TYPE="TEXT" NAME="TestText" onClick="formNa meTest(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.nam e.value' which gives "".
      > And finally, I tried passing 'this.form' to my fuction
      >(and modified my function) instead of 'this', with no luck.[/color]

      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.


      Comment

      • Scott Navarre

        #4
        Re: How to read Form name?

        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...


        Comment

        Working...