+ 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?
8 Answers
+ 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
+ 12
ODLNT Thanks that's exactly what I wanted.
+ 8
Ok then can I make them separately and call "sum()" inside the "calculator()" function?
+ 6
Does your sum() function has to be situated within calculator()? It is not possible to reach sum this way.
+ 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
+ 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.
+ 2
https://idratherbewriting.com/events-and-listeners-javascript/
i think this will help
+ 2
YG111 You're welcome.