0

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 the second. Anyway to have both in the onclick? It code be on the client side or in the code behind in C#.

Thanks!

5
  • that should work, what are you doing in the first function? Commented Jan 5, 2012 at 23:40
  • C# and Javascript have nothing to do with eachother. Commented Jan 5, 2012 at 23:40
  • possible duplicate of Can I have two JavaScript onclick events in one element? Commented Jan 5, 2012 at 23:40
  • 2
    In the example you've given, both functions will be called barring the first throwing an exception. Proof: jsbin.com/emohix Commented Jan 5, 2012 at 23:41
  • 2
    Yes you can, and this is a way to do it. If function2 is not called, then you have an error in function1. Commented Jan 5, 2012 at 23:41

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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();"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.