How can I pass multiple and unlimited arguments
and one or two parameters to a function?
Example:
function myFunction(_id, _class, arg1, arg2, arg3, arg4, etc...){
console.log($(_id).html());
console.log($(_class).html());
for(var i = 0; i < args.length; i++) {
alert(args[i]);
}
}
myFunction("#myDiv",".mySpan", "Hello World!", "Bonjour le monde!", "Hola mundo!", "Ciao mondo", "Hallo Welt!", "etc");
You can use the arguments object. It is an Array-like object corresponding to the arguments passed to a function.
function myFunction(_id, _class){
console.log($(_id).html());
console.log($(_class).html());
for(var i = 2; i < arguments.length; i++) {
alert(arguments[i]);
}
}
myFunction("#myDiv",".mySpan", "Hello World!", "Bonjour le monde!", "Hola mundo!", "Ciao mondo", "Hallo Welt!", "etc");
Demo: Fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With