How to call two methods on button's onclick method in HTML or JavaScript ?
Greetings! Yes, you can call two JS Function on one onClick.
So the answer is - yes you can :) However, I'd recommend to use unobtrusive JavaScript.. mixing js with HTML is just nasty.
To call multiple functions onClick in React:Set the onClick prop on the element. Call the other functions in the event handler function. The event handler function can call as many other functions as necessary.
The first solution to perform multiple onClick events in React is to include all of your actions inside of a function and then call that single function from the onClick event handler. Let's explore how to do that in a React Component: import React from 'react'; function App() { function greeting() { console.
Try this:
<input type="button" onclick="function1();function2();" value="Call2Functions" />
Or, call second function at the end of first function:
function func1(){ //--- some logic func2(); } function func2(){ //--- some logic }
...and call func1() onclick of button:
<input type="button" onclick="func1();" value="Call2Functions" />
As stated by Harry Joy, you can do it on the onclick
attr like so:
<input type="button" onclick="func1();func2();" value="Call2Functions" />
Or, in your JS like so:
document.getElementById( 'Call2Functions' ).onclick = function() { func1(); func2(); };
Or, if you are assigning an onclick programmatically, and aren't sure if a previous onclick existed (and don't want to overwrite it):
var Call2FunctionsEle = document.getElementById( 'Call2Functions' ), func1 = Call2FunctionsEle.onclick; Call2FunctionsEle.onclick = function() { if( typeof func1 === 'function' ) { func1(); } func2(); };
If you need the functions run in scope of the element which was clicked, a simple use of apply could be made:
document.getElementById( 'Call2Functions' ).onclick = function() { func1.apply( this, arguments ); func2.apply( this, arguments ); };
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