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!
Greetings! Yes, you can call two JS Function on one 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.
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.
js is single threaded. so no.
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
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();"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With