How can I use a function inside another function for onclick() event? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 14

How can I use a function inside another function for onclick() event?

function calculator(){ function sum(){ ...........; } } How can I make the "onclick()" event to go to "sum()"? Is it possible?

22nd Sep 2020, 12:33 AM
YG111
YG111 - avatar
8 Respostas
+ 7
Sure that is possible, you will have to call calculator() and pass it a value that will be used to determine which function is called within itself. Good luck. https://code.sololearn.com/WgwkqRoaDp3Y/#html
22nd Sep 2020, 8:31 AM
ODLNT
ODLNT - avatar
+ 12
ODLNT Thanks that's exactly what I wanted.
22nd Sep 2020, 11:30 AM
YG111
YG111 - avatar
+ 8
Ok then can I make them separately and call "sum()" inside the "calculator()" function?
22nd Sep 2020, 2:26 AM
YG111
YG111 - avatar
+ 6
Does your sum() function has to be situated within calculator()? It is not possible to reach sum this way.
22nd Sep 2020, 1:28 AM
Fermi
Fermi - avatar
+ 6
if you return the function from function then you can call it from outside other wise you have to call it from inside. method 1 shows how to return function from function and method 2 shows normal way. https://code.sololearn.com/WXFnZqy0Ku68/?ref=app https://code.sololearn.com/WNQ8apeKIpxD/?ref=app
22nd Sep 2020, 4:25 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 5
How to Call Multiple JavaScript Functions in onClick Event Answer: Use theĀ addEventListener()Ā Method If you want to call or execute multiple functions in a single click event of a button, you can use the JavaScriptĀ addEventListener()Ā method, like this: <script> // Defining custom functions function sayHello(){ alert("Hello World! sayHello() function executed successfully!"); } function sayHi(){ alert("Hi There! sayHi() function executed successfully!"); } // Selecting button element var btn = document.getElementById("myBtn"); // Assigning event listeners to the button btn.addEventListener("click", sayHello); btn.addEventListener("click", sayHi); </script> See the tutorial onĀ JavaScript event listenersĀ to learn more attaching multiple event handlers. Alternatively, you can also call more than one JavaScript function in oneĀ onclickĀ event.
22nd Sep 2020, 5:05 AM
Matias
Matias - avatar
22nd Sep 2020, 10:47 PM
Dove
Dove - avatar
+ 2
YG111 You're welcome.
23rd Sep 2020, 4:57 AM
ODLNT
ODLNT - avatar