10. Handling Forms

Table of Contents:

Handling Form Submissions

Original Form Submissions

But first...

Originally (and still with some frameworks) form submissions actually change the page.

See this difference by filling out the form in 1/old-form/original-way.html which redirects the user to the new page after submitting.

Q: See how the URL changes to results-page.html? Notice the stuff appended to the end?

results-page.html?username=ben&password=123

Old forms used a few attributes to achieve this behavior:

  • action = the new page to go to

  • method = "get" or "post" ("get" is default). "get" means we are requesting data using the form, "post" means we are sending data to be stored by the application

  • The form data becomes “query params” in the URL of the new page if you use "get"

This is the “default” behavior of the forms, which is NOT what we want.

The Modern Way

Instead of having the browser take us away from the page, most modern web applications use JavaScript to change the contents of the screen WITHOUT leaving the page.

To handle a form submission with JavaScript, we will need to:

  1. prevent the default behavior of navigating to a new page

  2. collect the form data using form.elements

  3. utilize the form data in some way (maybe render it to the screen, or send it to an API)

  4. reset the form

If these are our inputs:

Then, we can handle this form on the same page like this:

  • event.preventDefault() stops the form submission from redirecting/reloading the page

  • event.target is a reference the form element that caused the "submit" event to occur. We'll save it in a variable since we'll reference it a lot.

  • form.elements is an object containing all of the input elements of the form (the HTMLElement, not the value), accessible using the input name (e.g. form.elements.username or form.elements.age).

  • We can access the value of most input elements using the .value property (e.g. form.elements.username.value)

  • Checkboxes use the .checked property (e.g. form.elements.isHungry.checked).

  • form.reset() empties out the form!

Q: How can you improve this code so that you don't have to type out form.elements over and over again?

FormData API

Another, potentially faster, way to get the values of a form is to use the FormData API. It starts off the same, we have to grab the form using event.target:

Let's break this down:

  • We again store the form using event.target

  • We invoke the function new FormData() with that form as an argument

  • We immediately take the returned value and pass it to Object.fromEntries() which generates an Object with our form values!

One annoying gotcha: checkboxes

When using checkboxes, you would think they'd use a true/false setup, but nope! They use "on" and undefined.

So you'll need to do a little extra work to get them to be true/false:

By converting the formValues.isHungry property into a Boolean, it will be true if the value is "on" or false if the value is undefined.

Resetting

After you submit, sometimes you want to clear the form, so you can do that with form.reset() in the js, or on the form html itself, add a button with a type of reset

Tracking Input and Non Submission Events

You can use an input event on an input (or fieldset) to track the changes at a more granular level:

If you specify a type of button on a button, it will not trigger a submit automatically. This is a pretty rare occurrence, but it's good to know!

HTML:

JS:

Last updated