How to submit a form with JavaScript by clicking a link?

Spread the love

Sometimes, we want to submit a form with JavaScript by clicking a link.

in this article, we’ll look at how to submit a form with JavaScript by clicking a link.

How to submit a form with JavaScript by clicking a link?

To submit a form with JavaScript by clicking a link, we call the form submit method.

For instance, we write

<form id="form-id">
  <button id="your-id">submit</button>
</form>

to add a form and a button.

Then we write

const form = document.getElementById("form-id");

document.getElementById("your-id").addEventListener("click", () => {
  form.submit();
});

to select the form and the button with getElementById.

Next, we call addEventListener on the button to add a click listener to it.

In it, we call form.submit to submit the form.

Conclusion

To submit a form with JavaScript by clicking a link, we call the form submit method.

Leave a Reply

Your email address will not be published. Required fields are marked *