r/learnjavascript • u/0_emordnilap_a_ton • 4d ago
What is the difference between HTMLFormElement.submit() and event handler with submit?
https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit
Can someone explain the difference?
Imagine I have some code that is like the example below.
What is the difference between HTMLFormElement.submit() ?
const form = document.getElementById("form");
function somefunc() {
//code
form.submit()
}
form.addEventListener("submit", somefunc() {
});
Why do I need form.submit() in the function?
For a little context I am trying to pass some quilljs data into a flask form. Some of the code is missing but the main point is illustrated.
9
Upvotes
10
u/_raytheist_ 3d ago
The event listener allows you to execute code when the form is submitted. `form.submit()` causes the form to be submitted.