SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    May 2003
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JS and form name

    Hi,

    There is an (ASP/VBS) form that has 3 dropdown lists. Most of the code is actually JS, either hard coded or created by the VBS. The form works fine, does exactly what it should do.

    Now I need to 're-use' all the (JS) code in various ASP pages, which is easily done by the use of "includes". However, one problem; the original form (that works) had a form name, by the use of ID and NAME atrributes. The form was called 'FrmInfo", and therefore JS code used when the form loads, like:

    Code:
    var frm = null;
    ... etc,etc
         
    function init()
    {
      frm = document.getElementById('frmInfo');
      ....etc,etc
    }
    worked okay, and also any reference to the form like:

    Code:
    // populating a dropdown
    frm.selType.options[frm.selType.options.length] = new Option('--Select--', 'Sel');
    
    //using other JS function to re-populate other dropdowns
    getTypes(frm);
    getCategories(frm.selType.value);
    getSubCategories(frm.selCategory.value);
    but the problem I'm facing is that now, wanting to re-use the JS code in a number of ASP pages, using the JS code in an "include" references an actual form name (in the example above, the form name is 'FrmInfo' ).

    The ASP pages that need the JS code have forms with no names.

    There are two ways to address this (probably more ??):

    1. If I give the ASP pages form names, how do I pass the form name to the JS code, or more to the point, how can the JS code, go from:

    Code:
    frm = document.getElementById('frmInfo');
    to

    Code:
    frm = document.getElementById("PasstheformName");
    Well, ....... you get what I mean, I hope.

    2. The second method is to leave the ASP pages with no form names (preferred method, because it will reduce maintenance when the ASP 'product' has new releases). I have seen code like:

    Code:
    variableName = document.forms[0].fieldname.value
    and also the use of "this.name" . Is that how to reference fields and values in a form that has no name ? What are the best methods to do this ?

    Thanks,

    Peter

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Change this:
    Code:
    function init() { 
    frm = document.getElementById('frmInfo'); 
    ....etc,etc 
    }
    To this:
    Code:
    function init(formId) { frm = document.getElementById(formId); ....etc,etc }
    And call your function something like this:
    Code:
    // syntax is window.onload = init([yourformnamehere]);
    window.onload = init("myForm");

  3. #3
    SitePoint Evangelist
    Join Date
    May 2003
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JS and form name

    Hi vgarcia,

    Thanks for the suggestions, and sample code. I applied the changes, and used JS Console, and now getting an error message:

    frm has no properties
    It's in the following function:

    Code:
    function getTypes(frm)
      {
      frm.selType.options[frm.selType.options.length] = new Option('--Select--', 'Sel');
      //etc,etc
      }
    which is called by function "init"

    Code:
    function init(formId)
      {
      frm = document.getElementById(formId);
      getTypes(frm);
      getCategories(frm.selType.value);
      getSubCategories(frm.selCategory.value);
      }
    
      // syntax is window.onload = init([yourformnamehere]); 
      window.onload = init("frmInfo");
    I gave the form a name by:

    Code:
    <form action="postnew.asp" method="POST" name="frmInfo">
    Did I put the "window.onload" in the right place ? It is right at the end of all the JS code.

    Thanks,

    Peter

  4. #4
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    window.onload = init("frmInfo" );
    should be
    Code:
    window.onload = function() { init('frmInfo'); };

    (Calling init("frmInfo") like Vinnie posted causes it to be run when called, and not onload, because it is expecting to have a function returned to it)
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  5. #5
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    What Jeff said

    Also, you have to give your form an ID if you plan on using document.getElementById. Change your <form> tag to this:
    HTML Code:
    <form action="postnew.asp" method="POST"  id="frmInfo">

  6. #6
    SitePoint Evangelist
    Join Date
    May 2003
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JS and form name

    Hi Vinnie,

    Quote Originally Posted by vgarcia
    What Jeff said

    Also, you have to give your form an ID if you plan on using document.getElementById. Change your <form> tag to this:
    HTML Code:
    <form action="postnew.asp" method="POST"  id="frmInfo">
    FANTASTIC, that did it, thanks a million.

    Many thanks to you and Jeff.

    Peter


Bookmarks

Posting Permissions

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