How to call function in function in JS | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to call function in function in JS

Hello. I need to know, if there is any way to specify function written inside another function in form's onsubmit attribute. Here is simple example. I want to add lines into the table and I use function written inside window.onload function. Thanks for any reply. https://code.sololearn.com/Wk24LXi2D6Hf/?ref=app

18th Jun 2018, 4:45 PM
Jan Štěch
Jan Štěch - avatar
5 Answers
+ 5
For event listening function should be global. Use preventDefault() to prevent submit to external. And you don't need method="post" in form tag. Some error on appendChild https://code.sololearn.com/W96nJD1Z1ij9/?ref=app
18th Jun 2018, 5:29 PM
Calviղ
Calviղ - avatar
+ 2
you could add a #id to the form and then add event listeners the form and prevent the default of the form and do the logic https://developer.mozilla.org/en-US/docs/Web/Events/submit
18th Jun 2018, 4:58 PM
Ismael Garcia
Ismael Garcia - avatar
+ 1
THE MOST SIMPLE ANSWER YOU CAN GET <html> <body onload="x()"> //This will call function x <script> function x() { alert("function x here"); y(); //this will call function y } function y(){ alert(" Why did you call me?"); } </script> </body> </html>
19th Jun 2018, 5:43 AM
yyy
0
Ok, I managed to reach the function with: window.onload = function(){ var formSubmit = document.getElementById("formSubmit"); formSubmit.onclick = function addRow() { //Code to add new row } } But there is another problem. After the function finishes, the page reloads and returns to its non-modified state. How can I prevent it from reloading until the data are saved?
18th Jun 2018, 5:25 PM
Jan Štěch
Jan Štěch - avatar
0
Thank you both very much. You helped me a lot.
18th Jun 2018, 8:25 PM
Jan Štěch
Jan Štěch - avatar