Categories
JavaScript Answers

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

Spread the love

To submit a form with JavaScript by clicking a link, we can add a button and attach a click listener to it.

For instance, we write

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

to add a form.

Then we write

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

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

to select the form with getElementById.

And then we select the button with getElementById and add a click listener to it with addEventListener.

In the click listener, we call form.submit to submit the form.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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