Notes: Processing HTML Forms with JavaScript
Last updated
Last updated
HTML Forms use elements to collect data. Once the data has been collected, JavaScript can process it in the browser. Data processed in this way can be used to modify the DOM. Forms can also submit data to servers. In this course, we'll focus on processing the data in the browser and updating the DOM.
The input tag is used to render form elements. Its type property determines the element's presentation on the web page and the type of data it holds.
As shown in the Form Sampler, many types are available for the input element.
We'll focus on the input types of radio
and text
.
To access data in an input element from JavaScript, use the value
attribute. The value attribute can also be set in JavaScript. In the Input Sampler shown above, you can see data in the Number
and Password
fields. This is not value
data but placeholder
data. The placeholder attribute provides a way to add instructions to the user from within an input element.
The sample code below shows how to access the input data value
and type
using JavaScript.
Radio input buttons are rendered as tiny circles that are clickable. A set of radio buttons is usually expected to return a single value. Each radio button is rendered as a separate input. To create a set of related radio buttons, we add the name attribute, and all the radio buttons in a set get the same name.
The code below renders a set of radio buttons that allow the user to choose a size
from Small, Medium, or Large. We use the Label element to assign the selection values.
To get the value of size
we look for which item is checked.
The Select element presents a drop-down list of options for the user. The code below will render a select
element that allows the user to choose a size.
The code below demonstrates retrieving the data from a select element.
The label
tag renders a label element that provides a caption for an input. The label is not used to store or extract data from the form. It helps identify form elements to the user. It aids accessibility as screen readers can use it to provide meaning to the input.
In the example below, the ID of the input is bgcolor
. The label is linked to an input using its for attribute whose value is the input's id. In the examples above, inputs were identified by span and div, but this does not provide meaning. The label is an inline element. You can style it with CSS.
The fieldset
is used to group related items in a form. Like the label, It aids in accessibility. The fieldset can contain any input element at any time. In the example below, it is used to indicate radio button nut preference.
The code renders a list of radio buttons within a rectangle.
Processing the form data begins when the user clicks a button or other clickable element. In the examples we've been working with, the clickable element is an input
with type button
.
The click event handlers are triggered when the user clicks on the Update button, which has an ID of update
. Instructions to select inputs and extract data are executed inside the handler function. Like the window "load" event, the mouse "click" event is the function's first parameter.
We've looked at updating the DOM after the Windows load
event is triggered. We can update the DOM after data has been entered in a form and a click
event is triggered.
In this example, we allow the user to choose the font they want to see on their viewing page.
We can start with a default browser font family and then use the chosen one after the update.
On the left, we see the default font family; on the right, after clicking on the update button, we see the Gil Sans font family.