Welcome to the Geeks & God Static Archive. Read more »

Javascript and formname ?

Joined: 11/28/2008

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. /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

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 this example the form name is 'FrmInfo' ).

The ASP pages that need the JS code have forms with no names. /sad.gif" style="vertical-align:middle" emoid=":(" border="0" alt="sad.gif" />

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. /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

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

Is that how to reference fields and values in a form that has no name ??

Thanks,

Peter

"The church is never a place, but always a people;
never a fold but always a flock;
never a building but always a believing assembly.
The church is you who pray,
not where you pray."
-Anonymous

Joined: 11/28/2008
Hi PeterThat last bit of

Hi Peter

That last bit of Javascript is the way you can do it.

I am pretty sure, though, that you can (re)name a form dynamically (and set it's ID property too?), so you could do this:

CODE
document.forms[0].name = 'FrmInfo';

Perhaps right at the start (but after the form in the HTML has loaded so it exists).

That would then work with the rest of the code wouldn't it?

If not, ask again! /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" />

Paul

Paul Davey
Whitford Church
"Everyone who calls on the name of the Lord will be saved." Romans 10:13
"For all have sinned and fall short of the glory of God, and are justified

Joined: 11/28/2008
Hi Paul,What you

Hi Paul,

What you suggested:

QUOTE
CODE
document.forms[0].name = 'FrmInfo';

worked just fine, when I view PageInfo on the form, there is now a formname called "FrmInfo" , and all the fieldnames,types and values are there. Thanks. /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

Moving on from there, the 3 dropdowns that have been added (the ones populated by the JS code) are empty, and I _think_ it is just a 'timing' problem. Here is the generated source:

CODE
<head>
   <title>Page Title</title>
   <link rel="stylesheet" type="text/css" href="../default.css">
   
   <script language="javascript">
     function Category(type, name)
     {
       this.type = type;
       this.name = name;
     }

//  [SNIP] lots of JS functions and JS arrays filled correctly by ASP db stuff.

     var frm = null;
     
     function init()
     {
       frm = document.getElementById('frmInfo');
       getTypes(frm);           //populate the category group dropdown
       getCategories(frm.selType.value);    //populate the category dropdown
       getSubCategories(frm.selCategory.value);   //populate the subcategory one
     }
   </script>

 </head>
 <body onLoad="init();">
   <form action="postnew.asp" method="POST">
   <script language="javascript">
   document.forms[0].name = 'frmInfo';
   </script>
     <input type="hidden" name="uid" value="username">
     <div align="center">
       <table class="Wide">
         <tr>
...... all the fields setup as rows and cells within the table..... etc,etc

The JS function "init" populates the 3 dropdowns, but it is called before the form is given a name (... doh ! ). No doubt I can just move:

CODE
<script language="javascript">
   document.forms[0].name = 'frmInfo';
</script>

right up the top, just before the JS function "Category". (I didn't know what to do, but explaining it to you, made me realise my mistakes).

Many thanks,

Peter

"The church is never a place, but always a people;
never a fold but always a flock;
never a building but always a believing assembly.
The church is you who pray,
not where you pray."
-Anonymous

Joined: 11/28/2008
Hi,Well, ... so much for my

Hi,

Well, ... so much for my theory. The formname part is okay, but the JS function:

CODE
<body onLoad="init();">

is being called before the form is given a name, and as the function relies on the formname "frmInfo", it doesn't work. (At least all that appears to make sense to me). The sequence of code is:

CODE
<html  ...."html" info>
 <head>
   <title>Page Title</title>
   <link rel="stylesheet" type="text/css" href="../default.css">
   <!-- #Include File='../catdropdowns.asp' -->
 </head>
 <body onLoad="init();">

   <form action="postnew.asp" method="POST">
     <script language="javascript">
        document.forms[0].name = 'frmInfo';
     </script>
     <input type="hidden" name="uid" value="<% = uid %>">
     [SNIP]  ...... all the rest of the fieldnames,etc
   </form>
 </body>
<html>

The JS function "init" needs to be called after the form is given a name. Doing a quick Google, it looks like the ONLOAD attribute is only for BODY. Is there any method to force the "init" function to be run, AFTER the form has been given a name ? This problem is rather strange, because when ALL the ASP,JS,HTML was in one ASP file, the sequence of the code was:

CODE
<head>
 <title>Dropdown Test</title>
 <link rel="stylesheet" type="text/css" href="default.css">
 <script language="javascript">
     function Category(type, name)
     {
     // all the other JS stuff
 </script>

</head>
 <body onLoad="init();">
   <form id="frmInfo" name="frmInfo">
     Category Groups:
          ...... all the other fieldnames, rows, etc,etc
   </form>
 </body>

and the dropdowns were populated fine, the form had a name of 'frmInfo'. Should I also add:

CODE
<script language="javascript">
 document.forms[0].id = 'frmInfo';
</script>

I guess before everything was within the FORM element, but now it's quite different. I'm pulling my hair out on this and I'm already bald. /rolleyes.gif" style="vertical-align:middle" emoid=":rolleyes:" border="0" alt="rolleyes.gif" />

Many thanks, /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

Peter

"The church is never a place, but always a people;
never a fold but always a flock;
never a building but always a believing assembly.
The church is you who pray,
not where you pray."
-Anonymous

Joined: 11/28/2008
Hi PeterCan you incorporate

Hi Peter

Can you incorporate it with the onLoad event (or even the init function), example:

CODE
onLoad="if (document.forms[0].name=='') { document.forms[0].name=='frmInfo'; } init();"

That worked for me. It tests that the form doesn't already have a name.

Paul Davey
Whitford Church
"Everyone who calls on the name of the Lord will be saved." Romans 10:13
"For all have sinned and fall short of the glory of God, and are justified

Joined: 11/28/2008
Gidday Paul,Apparently,

Gidday Paul,

Apparently, because I'm using document.getElementById , then I need to give the form an ID as well, like:

CODE
<form action="postnew.asp" method="POST" id="frmInfo">

...... and now the form works just fine. /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

I didn't work this out though, see JS and formname

Many thanks for your help,

Peter

"The church is never a place, but always a people;
never a fold but always a flock;
never a building but always a believing assembly.
The church is you who pray,
not where you pray."
-Anonymous