Skip to content

Handling forms in JavaScript

Discover the basics of working with forms in HTML and JavaScript

Forms are an extremely important part of HTML and the Web Platform. They allow users can interact with the page and

and much much more.

By default, forms submit their content to a server-side endpoint, which by default is the page URL itself:

<form>
  ...
  <input type="submit" />
</form>

We can override this behavior by setting the action attribute of the form element, using the HTML method defined by the method attribute, which defaults to GET:

<form action="/contact" method="POST">
  ...
  <input type="submit" />
</form>

Upon clicking the submit input element, the browser makes a POST request to the /contact URL on the same origin (protocol, domain and port).

Using JavaScript we can intercept this event, submit the form asynchronously (with XHR and Fetch), and we can also react to events happening on individual form elements.

Intercepting a form submit event

I just described the default behavior of forms, without JavaScript.

In order to start working with forms with JavaScript you need to intercept the submit event on the form element:

const form = document.querySelector('form')
form.addEventListener('submit', (event) => {
  // submit event detected
})

Now inside the submit event handler function we call the event.preventDefault() method to prevent the default behavior and avoid a form submit to reload the page:

const form = document.querySelector('form')
form.addEventListener('submit', (event) => {
  // submit event detected
  event.preventDefault()
})

At this point clicking the submit event button in the form will not do anything, except giving us the control.

Working with input element events

We have a number of events we can listen for in form elements

Here’s a sample form demo on Codepen: https://codepen.io/flaviocopes/pen/zQrqNy/


download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about platform: