View Full Version : How do I dynamically identify the forms on a document?

07-12-2005, 09:39 PM

A single page having two forms... form1 and form2
A left nav of links.

The links all call a javascript function called "triggerSubmit()".

Based on the link selected, I need to save a unique value to a form.

the function:
function triggerSubmit(someValue) {

The link:
<a href="javascript:triggerSubmit('gblNavigationAction');">Link Text</a>

Because the left nav is included on many pages each having it's own form(s),
How can I make the function triggerSubmit more dynamic to determine the form names on a page? -and/or- if no form exists how do I check that also with the mindset of being more dynamic and not hard coded... I don't wish to copy the function on every page and customize it.

Any tips would be appreciated...
Thanks upfront for your assist!

Mr J
07-12-2005, 10:38 PM
Something on these lines maybe?

function triggerSubmit(someValue) {



alert("There are no forms")


for(var i=0;i<numberOfForms;i++){

if(document.forms[i].name == "yourFormName"){




07-12-2005, 10:54 PM

The simple example I've written below should help you. The forms can be accessed through the javascript forms array. This array is accessed through the document object,

document.forms.length // number of forms on page

and to access the name of each form you use the name property of the form,

document.forms[0].name // name of first form on page.

The forms are numbered from 0 up to 1 less than the number of forms on the page.


var num_forms
var form1
var form2

function countforms() {
num_forms = document.forms.length;
form1 = document.forms[0].name;
form2 = document.forms[1].name;



<FORM NAME="form1">

<FORM NAME="form2">

<INPUT TYPE=BUTTON VALUE="count forms" ONCLICK="countforms();">

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum