[thelist] js function with variable parameters...need advice

Scott Dexter sgd at ti3.com
Mon Feb 10 16:27:01 CST 2003

Tom Dell'Aringa wrote on Monday, February 10, 2003 4:04 PM:
> Hey Volters,
> I have a situation where a user changes a dropdown from "no"
> to "yes". When "yes" is selected a *variable* number of
> fields are changed from disabled to enabled to allow them to further
> fill in data.

> I have probably *twenty* of these to handle on one page. You
> can see the function works fine, but how can I make it so I
> can pass any number of field names (actually like 1-3 depending on)?

One of the cool* things about javascript is you don't have to prototype
the number of parameters; you can define a function:

function DoSomething() {

and still pass in parameters:

DoSomething(val1, val2, val3)

Or, from the Netscape Docs

Using the arguments Array

The arguments of a function are maintained in an array. Within a
function, you can address the
parameters passed to it as follows:


where functionName is the name of the function and i is the ordinal
number of the argument,
starting at zero. So, the first argument passed to a function named
myfunc would be
myfunc.arguments[0]. The total number of arguments is indicated by the

Using the arguments array, you can call a function with more arguments
than it is formally
declared to accept using. This is often useful if you don't know in
advance how many arguments
will be passed to the function. You can use arguments.length to
determine the number of
arguments actually passed to the function, and then treat each argument
using the arguments

For example, consider a function defined to create HTML lists. The only
formal argument for the
function is a string that is "U" for an unordered (bulleted) list or "O"
for an ordered (numbered) list.
The function is defined as follows:

function list(type) {
   document.write("<" + type + "L>") // begin list
   // iterate through arguments
   for (var i = 1; i < list.arguments.length; i++)
      document.write("<LI>" + list.arguments[i])
      document.write("</" + type + "L>") // end list

You can pass any number of arguments to this function, and it will then
display each argument as
an item in the indicated type of list. For example, the following call
to the function

list("o", "one", 1967, "three", "etc., etc...")

results in this output:

1. one
2. 1967
3. three
4. etc., etc...

* I say "cool" in the sense that it's a neat shortcut. It does fly in
the face of maintainability and readability, but sometimes
(/sometimes/), that's okay

TALX FasTime Services
Customized Systems for H.R., Payroll, and Employee Self Service
	*	Telephone/Internet Time and Attendance
	*	Telephone/Internet Surveys
	*	Automated Outbound Calling and Messages

> Tom
> =====
> var me = tom.pixelmech.webDeveloper();
[Making A Commercial Case for Adopting Web Standards]

Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com --
* * Please support the community that supports you.  * *

For unsubscribe and other options, including the Tip Harvester
and archives of thelist go to: http://lists.evolt.org
Workers of the Web, evolt !

More information about the thelist mailing list