3.7 Forms

An HTML form is used to collect user input. The user input can then be sent to a server for processing.

The <form> Element

The HTML <form> element defines a form that is used o collect user input. An HTML form contains form elements. Form elements are different types of input elements, like text fields, checkboxes, radio buttons, submit buttons and more.

The <input> Element

The <input> element is the most important form element, The <input> element is displayed in several ways, depending on the type attribute.

Text Fields

<input type ="text"> defines a single-line input field for text input.

Note: The form itself is not visible. Also note that the default width of an input field is 20 characters.

Input Type Checkbox

<input type="checkbox"> defines a checkbox. Checkboxes let a user select Zero or More options of a limited number of choices.

The <label> Element

The <label> tag defines a label for many form elements. The<label> element is useful for screen-reader users because the screen-reader will read out loud the label when the user is focused on the input element.

The<label> element also help users who have difficulty clicking on very small regions(such as radio buttons or checkboxes) - because when the user clicks the text within the <label> element, it toggles the radio button/checkbox.

The for attribute of the <label> tag should be equal to the id attribute of the <input> element to bind them together.

The <select> Element

The<select> element defines a drop-down list.

The<option> elements define an option that can be selected. By default, the first item in the drop down list is selected. To define a pre-selected option, add the selected attribute to the option:

The <textarea> Element