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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2003
    Posts
    601
    Thanks
    17
    Thanked 0 Times in 0 Posts

    JS: Form Reference Question (Quick and Easy)

    Hey Guys,

    Having a senior moment here. I have the following JS reference to a form:

    document.forms['formLogin'].submit();

    I want to use this code across other pages with different form names, so what's the easiest way to grab the form name from the page, and reference it in the above line of code? Something close to:

    var formName = this.form; (this doesn't work)
    document.forms[formName].submit();


    Any suggestions would be appreciated.

    Thanks.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    Why you don't use the submit method of the form object?

    <input type = "text" name="txt1" size = "20" onchange="this.form.submit()">

    or whatever

  • #3
    Regular Coder
    Join Date
    Jul 2003
    Posts
    601
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Because I'm doing some field checking in the form before I submit it ..

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    The general idea is:-

    Code:
    <form name = "myform" method = "POST" action = "url">
    <input type = "text" name="txt1" id = "txt1" size = "20" onchange = "if (validate(this)) {this.form.submit()}">
    </form>
    
    <script type = "text/javascript">
    function validate(which) {
    x = which.value;
    if (x != "Philip") {
    return false;
    }
    return true;
    }
    </script>

  • #5
    Banned
    Join Date
    May 2005
    Location
    Midwest, U.S.
    Posts
    118
    Thanks
    1
    Thanked 26 Times in 23 Posts
    Use:

    document.forms[0].submit;

    The first set of <form></form> tags, reading the code from top to bottom will always be the ordinal number zero.

    You can address a form, and other tag sets, either by name or ordinal number.

    If the "name" of the first form is, for example, "user_login", then the following are equivalent.

    document.forms[0].submit();
    document.forms['user_login'].submit();

  • Users who have thanked Cranford for this post:

    kraftomatic (08-14-2008)

  • #6
    Regular Coder
    Join Date
    Jul 2003
    Posts
    601
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Cranford View Post
    Use:

    document.forms[0].submit;

    The first set of <form></form> tags, reading the code from top to bottom will always be the ordinal number zero.

    You can address a form, and other tag sets, either by name or ordinal number.

    If the "name" of the first form is, for example, "user_login", then the following are equivalent.

    document.forms[0].submit();
    document.forms['user_login'].submit();
    Thanks Cranford. I was looking to grab the name of the form, but the numerical reference was one I had forgotten but will work.

    Cheers.


  •  

    Posting Permissions

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