You can manually submit forms through JavaScript using the submit()
and requestSubmit()
methods.
Submitting forms through JavaScript gives you more control. You can prevent default behaviors, validate the form, or process inputs in JavaScript before the form is sent to the server.
HTMLFormElement.submit()
The submit()
method does not accept any parameters and does not return anything.
Note: Invoking
submit()
does not raise asubmit
event; the form’sonsubmit
handler is not run.
HTMLFormElement.requestSubmit(submitter)
submitter
: (Optional) The submit button used to handle form submission. If not provided, the form element itself is used as submitter.The requestSubmit()
method is used when you want to activate the form’s submit <button>
or raise the form’s onsubmit
handler.
Let’s try submitting a form using an image.
submit()
Notice how the form’s onsubmit
handler is not called.
requestSubmit()
The form’s onsubmit
handler is called. Here we use it to prevent page reload on form submission.
Free Resources