Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing different number of arguments to a function. 1

Status
Not open for further replies.

max1x

Programmer
Jan 12, 2005
366
0
0
US
I have function, which depending on how it's being called can have different number of arguments passed to it. I know how to set it up, when I know the # of args being passed, but not sure how to handle multiples.


Code:
function tryThis(val1, val2) {
  var myVal1 = val1; 
  var myVal2 = val2;
  alert (val1 + val2);
}

How to pass multiple values?

Code:
fucntion tryMul(?) {

}
 
something like this:?

Code:
<script language="javascript">
	function foo() {
		for (var i = 0 ; i < arguments.length ; i++) {
			alert(arguments[i]);
		}
	}
</script>
<a href="javascript:foo('apples','oranges','pears');">3 Fruits</a><br />
<a href="javascript:foo('Canada','USA','Mexio','England','France');">5 Countries</a><br />
<a href="javascript:foo('English','French','Spanish','Chinese');">4 Languages</a>

--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
Worked great, I modified it match my needs. Star for your effort.

Are there any downsides to this approach?
 
no downsides as far as I know, the "arguments object" is included with every js function for this exact purpose.

Others may have experienced some sort of difficulties with it, but it's always worked find for me.

--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top