+ 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
5 ответов
+ 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
+ 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
+ 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>
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?
0
Thank you both very much. You helped me a lot.