Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Gil
    Gil is offline
    New Coder
    Join Date
    Jul 2002
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Submitting a form with a html link

    Hi,

    Can someone explain the next thing to me. I can submit a form with the following button and pas a value for the field "testfield":

    <input type="submit" Onclick="this.form.testfield.value='myvalue'">

    If I want to do the same with a textual html link it will only work if the form has a name, so I gave the form the name test:

    <a href="#" Onclick="test.testfield.value='mysecondvalue';test.submit()">Submit</a>

    I must be doing something wrong if this isn't a 'feature' in HTML

    I I use:

    <a href="#" Onclick="this.form.testfield.value='myvalue';this.form.submit()">Submit</a>

    it will not be submitted.

    Can someone explain this to me so I can make it work?

    ....thanks a BIG lot,

    Gil

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In order for this.form to function, the object must be part of the form's elements collection, which any anchor object is not (only INPUT, TEXTAREA, SELECT). Instead, you can reference the form by it's index in the document's form collection:

    document.forms[0].submit();

    This, of course, assumes that the form is either the only form on the page, or the first form to appear in the HTML.

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    this, inside any HTML event handler, refers to the element (object) the handler is a property of. this.form works for form elements, which have a specific property named .form, exposed for just this purpose - to let you step 'up one level' to the Form object. this within a Link object's event handler refers to - what else - the Link object; no .form property there. Your posted link should work in Internet Explorer, which exposes ids/names as global variables; for cross-browser purposes:

    <a href="#" onclick="document.test.testfield.value='mysecondvalue';document.test.submit()">Submit</a>

    window is implicit; document ain't.

  • #4
    Gil
    Gil is offline
    New Coder
    Join Date
    Jul 2002
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks a loth to the both of you. It's clear to me now.

    Gil


  •  

    Posting Permissions

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