155

HTML & JS

How do I call 2 functions from one onclick event? Here's my code

 <input id ="btn" type="button" value="click" onclick="pay() cls()"/>

the two functions being pay() and cls(). Thanks!

2
  • 37
    Hello semicolon, dear statement terminator. pay(); cls() Commented Apr 15, 2013 at 21:40
  • 4
    What's inside your quotes is just javascript. Separate java statements using a semicolon. ie: pay(); cls(); Commented Apr 15, 2013 at 21:40

9 Answers 9

283

Add semi-colons ; to the end of the function calls in order for them both to work.

 <input id="btn" type="button" value="click" onclick="pay(); cls();"/>

I don't believe the last one is required but hey, might as well add it in for good measure.

Here is a good reference from SitePoint http://reference.sitepoint.com/html/event-attributes/onclick

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

Comments

57

You can create a single function that calls both of those, and then use it in the event.

function myFunction(){
    pay();
    cls();
}

And then, for the button:

<input id="btn" type="button" value="click" onclick="myFunction();"/>

3 Comments

it can be done, but seems to me it is not so transparent or it could be named similar to something like funtion1_function2(), in a case that funcitons are not related.
won't it have more overhead than just doing onclick="pay(); cls();" ?
@Am33d an overload that translates as one cell in the stack and half a millisecond extra for processing. It is more than worth it for the gain in readability and easy of maintenance. Imagine that in the future you have to add three extra methods, with conditional logic.
23

You can call the functions from inside another function

<input id ="btn" type="button" value="click" onclick="todo()"/>

function todo(){
pay(); cls();
}

1 Comment

It's been more than a year since this answer, and only now I noticed we posted the same answer at almost the same time (but you beat me by a minute, so have my +1).
14
onclick="pay(); cls();"

however, if you're using a return statement in "pay" function the execution will stop and "cls" won't execute,

a workaround to this:

onclick="var temp = function1();function2(); return temp;"

Comments

12

Binding events from html is NOT recommended. This is recommended way:

document.getElementById('btn').addEventListener('click', function(){
    pay();
    cls();
});

Comments

10

With jQuery :

jQuery("#btn").on("click",function(event){
    event.preventDefault();
    pay();
    cls();
});

Comments

10

Just to offer some variety, the comma operator can be used too but some might say "noooooo!", but it works:

<input type="button" onclick="one(), two(), three(), four()"/>

http://jsbin.com/oqizir/1/edit

Comments

6

Try this

<input id ="btn" type="button" value="click" onclick="pay();cls()"/>

Comments

6

put a semicolon between the two functions as statement terminator.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.