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:
public/form.html<form action="/handler/" method="POST">
<input name="test1">
<input name="test2">
</form>
<button>submit</button>
We get references to our elements into variables:
public/script.jslet form = document.querySelector('form');
let button = document.querySelector('button');
Add a click handler to the button:
public/script.jsbutton.addEventListener('click', function(event) {
});
And by clicking on the button,
we will send the form using the
submit method:
public/script.jsbutton.addEventListener('click', function(event) {
form.submit();
});
Задание 1
Given a form with three inputs. Provide a link that will be clicked to submit the form.