this.form.name -> error?

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

    this.form.name -> error?

    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.f orms[0].name" works correctly.

    <form name='whatever1 ' method = "post"
    action="javascr ipt:alert(this. form.name);">
    <input type="button" value="click" onClick = "this.form.subm it();"/>
    </form>

    Any ideas welcome,

    AS

  • web.dev

    #2
    Re: this.form.name -&gt; error?


    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.f orms[0].name" works correctly.
    >
    <form name='whatever1 ' method = "post"
    action="javascr ipt: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.subm it();"/>
    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.nam e)">
    <input type = "submit" value = "click">
    </form>

    Remember to do the appropriate handling for submit, as it stands it
    will still submit the form.

    Comment

    • ashore

      #3
      Re: this.form.name -&gt; error?

      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

      Comment

      • web.dev

        #4
        Re: this.form.name -&gt; error?


        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.nam e)">
        <input type = "submit" value = "click">
        </form>
        </body>
        </html>

        Comment

        • ashore

          #5
          Re: this.form.name -&gt; error?

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

          AS

          Comment

          Working...