Call two functions from the same onclick

HTML and JS

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

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

two functions: pay () and cls (). Thank you

+69
javascript html onclick
Apr 15 '13 at 21:38
source share
9 answers

Add half columns ; at the end of function calls so that they both work.

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

I do not believe that the latter is needed, but hey, can also add it for a good measure.

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

+122
Apr 15 '13 at 21:41
source share

You can create one function that calls both of them, and then use it in an event.

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

And then, for the button:

 <input id="btn" type="button" value="click" onclick="myFunction();"/> 
+29
Apr 15 '13 at 21:41
source share

You can call functions from another function

 <input id ="btn" type="button" value="click" onclick="todo()"/> function todo(){ pay(); cls(); } 
+13
Apr 15 '13 at 21:40
source share

Using jQuery:

 jQuery("#btn").on("click",function(event){ event.preventDefault(); pay(); cls(); }); 
+7
Apr 15
source share

To suggest several options, you can use the comma operator , but some may say "noooooo!", But it works:

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

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

+7
Apr 15 '13 at 21:47
source share

Html binding events are NOT recommended. This is recommended:

 document.getElementById('btn').addEventListener('click', function(){ pay(); cls(); }); 
+6
Jul 12 '14 at 18:07
source share
 onclick="pay(); cls();" 

however, if you use the return statement in the pay function, execution will stop and cls will not execute,

workaround:

 onclick="var temp = function1();function2(); return temp;" 
+5
Apr 15 '13 at 21:47
source share

puts a semicolon between two functions as an operator terminator.

+5
Apr 15 '14 at 10:08
source share

try it

 <input id ="btn" type="button" value="click" onclick="pay();cls()"/> 
+4
Apr 15
source share



All Articles