...

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



larsonr
07-12-2005, 09:39 PM
Situation:

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) {
document.formName.uniqueValue.value=someValue;
document.formName.submit();
}

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!
-Ron

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



function triggerSubmit(someValue) {

numberOfForms=document.forms.length

if(numberOfForms==0){

alert("There are no forms")

}
else{

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

if(document.forms[i].name == "yourFormName"){
document.forms[i].uniqueValue.value=someValue;
document.form[i].submit();
}

}

}

}

sparks80
07-12-2005, 10:54 PM
Hi,

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.


Example:

<HTML>
<HEAD>
<SCRIPT>
<!--
var num_forms
var form1
var form2

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

//-->
</SCRIPT>

</HEAD>

<BODY>
<FORM NAME="form1">
</FORM>

<FORM NAME="form2">
</FORM>

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum