Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call two functions from same onclick [duplicate]

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!

like image 986
user182 Avatar asked Oct 09 '22 23:10

user182


People also ask

Can we call 2 functions in onClick?

Greetings! Yes, you can call two JS Function on one onClick.

How do you call multiple functions onClick?

Given multiple functions, the task is to call them by just one onclick event using JavaScript. Here are few methods discussed. Either we can call them by mentioning their names with element where onclick event occurs or first call a single function and all the other functions are called inside that function.

How do you link two functions in JavaScript?

You can't 'merge' functions as you describe them there, but what you can do is have one function be redefined to call both itself and a new function (before or after the original). var xyz = function(){ console. log('xyz'); }; var abc = function(){ console.

Can we run two functions simultaneously in JavaScript?

js is single threaded. so no.


2 Answers

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

like image 263
Chris Bier Avatar answered Oct 11 '22 13:10

Chris Bier


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();"/>
like image 52
Geeky Guy Avatar answered Oct 11 '22 11:10

Geeky Guy