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=123Old forms used a few attributes to achieve this behavior:
action= the new page to go tomethod="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 applicationThe 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:
prevent the default behavior of navigating to a new page
collect the form data using
form.elementsutilize the form data in some way (maybe render it to the screen, or send it to an API)
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 pageevent.targetis a reference theformelement that caused the"submit"event to occur. We'll save it in a variable since we'll reference it a lot.form.elementsis an object containing all of theinputelements of the form (the HTMLElement, not the value), accessible using the inputname(e.g.form.elements.usernameorform.elements.age).We can access the value of most
inputelements using the.valueproperty (e.g.form.elements.username.value)Checkboxes use the
.checkedproperty (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
formusingevent.targetWe invoke the function
new FormData()with thatformas an argumentWe 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