bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/HTML/Forms and Input
HTML•Forms and Input

HTML Form Elements

Concept visual

HTML Form Elements

Pointer walk
two pointers
leftright102132436485116
left=0
right=6
1
3

Start at both ends

This chapter describes all the different HTML form elements.

Formula

The HTML < form > Elements

The HTML

<form> element can contain one or more of the following form elements: <input> <label> <select> <textarea> <button> <fieldset> <legend> <datalist> <output> <option> <optgroup>

Formula

The < input > Element

One of the most used form elements is the <input> element.

The

<input> element can be displayed in several ways, depending on the type attribute.

Example

<label for="fname">First name:</label> <input type="text" id="fname" name="fname"> All the different values of the type attribute are covered in the next chapter: HTML Input Types.

Formula

The < label > Element

The

<label> element defines a label for several form elements.

The

<label> element is useful for screen-reader users, because the screen-reader will read out loud the label when the user focus 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>

Formula

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.

Formula

The < select > Element

The

<select>

Formula

element defines a drop - down list:

Example

<label for="cars">Choose a car:</label> <select id="cars" name="cars"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="fiat">Fiat</option> <option value="audi">Audi</option> </select>

The

<option> element defines an option that can be selected.

Formula

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:

Example

Formula

< option value ="fiat" selected > Fiat </option >

Visible Values:

Use the size attribute to specify the number of visible values:

Example

<label for="cars">Choose a car:</label> <select id="cars" name="cars" size="3"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="fiat">Fiat</option> <option value="audi">Audi</option> </select>

Allow Multiple Selections:

Use the multiple attribute to allow the user to select more than one value:

Example

<label for="cars">Choose a car:</label> <select id="cars" name="cars" size="4" multiple> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="fiat">Fiat</option> <option value="audi">Audi</option> </select>

Formula

The < textarea > Element

The

<textarea>

Formula

element defines a multi - line input field (a text area):

Example

<textarea name="message" rows="10" cols="30"> The cat was playing in the garden. </textarea> The rows attribute specifies the visible number of lines in a text area. The cols attribute specifies the visible width of a text area. This is how the HTML code above will be displayed in a browser: The cat was playing in the garden. You can also define the size of the text area by using CSS:

Example

<textarea name="message"

style="width:200px; height:600px;">

The cat was playing in the garden. </textarea>

Formula

The < button > Element

The

<button> element defines a clickable button:

Example

<button type="button" onclick="alert('Hello World!')">Click Me!</button> This is how the HTML code above will be displayed in a browser: Click Me!

Note:

Always specify the type attribute for the button element. Different browsers may use different default types for the button element.

Formula

The < fieldset > and < legend > Elements

The

<fieldset> element is used to group related data in a form.

The

<legend> element defines a caption for the <fieldset> element.

Previous

HTML Form Attributes

Next

HTML Form Elements Code Challenge