JavaScript - Passing Arguments

G

Galsaba

I want to use checkForm function for 10 forms I have.
form1, form2....., form10.
How can I pass argument to
....
if (document.formX.quantity.value>10)
....
(formX will be form1 or form2, ...or form10 etc.)

function checkForm(P1,P6,P11)
{
if (document.form1.quantity.value>10)
{
document.form1.amount.value=P11
}
else
{
if (document.form1.quantity.value>5)
{
document.form1.amount.value=P6
}
else
{
document.form1.amount.value=P1
}
}
}
 
E

Eric Bohlman

(e-mail address removed) (Galsaba) wrote in m04.aol.com:
I want to use checkForm function for 10 forms I have.
form1, form2....., form10.
How can I pass argument to
...
if (document.formX.quantity.value>10)
...
(formX will be form1 or form2, ...or form10 etc.)

Use the "collection" syntax:

if (document.forms['formX'].quantity.value>10)

The index to document.forms[] is a string, so you can build it up by
concatenation:

function checkForm(n,P1,P6,P11)
{var whichform=document.forms['form'+n];
if (whichform.quantity.value>10)
{whichform.amount.value=P11
}
etc.
 
R

Richard Cornford

Eric said:
(e-mail address removed) (Galsaba) wrote:
if (document.formX.quantity.value>10)
...
(formX will be form1 or form2, ...or form10 etc.)

Use the "collection" syntax:

if (document.forms['formX'].quantity.value>10)
<snip>

It is misleading to refer to this a '"collection" syntax', as javascript
has no notion of "collection", that is a host environment (usually
browser) construct. While the syntax is categorised by ECMA 262 as a
"bracket notation property accessor", and, as such, is a core language
feature; a direct alternative to dot notation property accessors that
apply in any, and all, named property resolution contexts in javascript.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top