Submitting a form via JavaScript

It is possible to force a form submission via JavaScript. Let's see how it's done. Suppose we have a form and some button, on click on which we want to submit the form:

<form action="/handler/" method="POST"> <input name="test1"> <input name="test2"> </form> <button>submit</button>

We get references to our elements into variables:

let form = document.querySelector('form'); let button = document.querySelector('button');

Add a click handler to the button:

button.addEventListener('click', function(event) { });

And by clicking on the button, we will send the form using the submit method:

button.addEventListener('click', function(event) { form.submit(); });

Given a form with three inputs. Provide a link that will be clicked to submit the form.

enru