Skip to content Skip to sidebar Skip to footer

Two Javascript Function In An Onclick

Is it possible to have two function calls in a onclick? I tried doing something like: onclick='function1(); function2();' Doing that it only executes the first function, but not t

Solution 1:

I'd say either function1 or function2 is undefined, or function1 is throwing an exception, or (with respect) you're simply wrong that both of them don't get called. In the normal case, both will be called (proof).

That's not to say it's a good idea. You'd be much better off defining a third function that calls the other two, or even better using DOM2 handlers and doing away with onclick entirely.

Solution 2:

That should work. You may have an error in function1() which causes everything after it to not be executed.

Solution 3:

That should be fine unless you have an error in one of your functions. However, I encourage other developers to have one function that calls both functions, then call it in your onclick event.

function function3()
   {
     function1();
     function2();
   }

   ... onclick="function3();"

Post a Comment for "Two Javascript Function In An Onclick"