www.webdeveloper.com
Results 1 to 5 of 5

Thread: this.form.name is undefined

  1. #1
    Join Date
    Nov 2009
    Posts
    3

    Unhappy this.form.name is undefined

    Hi all,
    sorry for this simple problem, but I seem to not be able to see the wood for all of the trees...

    the following is a simpler version of a script that I am working on and I am getting the error: this.form.name is undefined.
    Obviously I do have a name and an id...
    What's the matter???

    <form id="form_2_update_for_72" name="form_2_update_for_72" method="POST">
    <a onclick="alert(this.form.name)">show me this.form.name...</a>
    </form>

    <hr>

    <form id="form_2_update_for_73" name="form_2_update_for_73" method="POST">
    <a onclick="alert(this.form.name)">show me this.form.name...</a>
    </form>

    Thanks for any help.

  2. #2
    Join Date
    Jul 2009
    Posts
    72
    "this" refers to the object tasked to perform the event, here you are refering "this" to the link that performs the onclick event, thus trying to display the name of the links object, "form", which does not exist.

    If you want to get the name of the form that the link is a child to, you could try climbing up the dom tree with something like alert(this.parentNode.name) or something, but this can give different results in different browsers.

    Try telling us what you are trying to do with the code, why u need the name and when to get it, maybe it will be easier to figure out...

  3. #3
    Join Date
    Nov 2009
    Posts
    3
    Quote Originally Posted by Reis View Post
    "this" refers to the object tasked to perform the event, here you are refering "this" to the link that performs the onclick event, thus trying to display the name of the links object, "form", which does not exist.

    If you want to get the name of the form that the link is a child to, you could try climbing up the dom tree with something like alert(this.parentNode.name) or something, but this can give different results in different browsers.

    Try telling us what you are trying to do with the code, why u need the name and when to get it, maybe it will be easier to figure out...
    the form I am actually using is generated on the fly via php and a smarty template. The thing is that this particular function (a calendar field) works just fine in all the other modules and I was surprised that it doesn't work here, however I was able to get the same error message as from my actual form field...

    Essentially I have got a set of fields that lead to a calendar control using javascript to call the respective functions and format the output.
    I thought "this" usually points to the current form...probably have been mistaken :-/
    Nevertheless how would I reference the actual form that contains my current field...?

  4. #4
    Join Date
    Nov 2009
    Posts
    3
    Quote Originally Posted by diwu View Post
    the form I am actually using is generated on the fly via php and a smarty template. The thing is that this particular function (a calendar field) works just fine in all the other modules and I was surprised that it doesn't work here, however I was able to get the same error message as from my actual form field...

    Essentially I have got a set of fields that lead to a calendar control using javascript to call the respective functions and format the output.
    I thought "this" usually points to the current form...probably have been mistaken :-/
    Nevertheless how would I reference the actual form that contains my current field...?
    Hah, I figured it out...
    The solution is instead of this.form
    Code:
    document.getElementById('whatever_field_id')
    All the best!!!

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Code:
    <a onclick="alert(this.form.name)">
    The answer is simple. A link is not one of the form's elements (controls). A form can have, as his controls, only: INPUT, TEXTAREA, SELECT, BUTTON and (with some crossbrowser differences) OBJECT elements.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles