0

Suppose I have a function

function foo(a, b, c)
{
   // ...
} 

that I want a button to call. I know that the way to call foo if it were a parameterless function would be

onclick="foo()"

but what is the way to do it when the function has parameters? Is it simply

onclick="foo(a,b,c)"

????

That seems awkward to have a string that gets interpreted as a function call. And does that even work? Or what is the way to do what I'm trying to do?

user3537336
  • 221
  • 2
  • 9

2 Answers2

0

Your example would be the correct way to do it, assuming a, b, and c are variables defined in the window scope. To pass specific values for a, b, and c you'd do something like onclick="foo('bar', 'baz', 'quux')". The "magic" form onclick="foo(event)" will pass in the Event object containing details about e.g. what button was clicked and where.

tgies
  • 694
  • 4
  • 19
0

You can't call without parameters, unless you do this :

function foo(/*a,b,c*/){
    some stuff
}

Or

function foo(){
    some stuff
}

Or

onclick="foo(a,b,c)"

So you assign some value to use the function.

Jamie
  • 1,096
  • 2
  • 19
  • 38