HTML Forms

HTML Tutorial

HTML Forms

HTML Graphics

HTML Media

HTML API

HTML Forms

An HTML form collects the user input. The user input is often sent to a server for processing.

Example:

				
					<form action="#">
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname">
  <label for="fname">Last name:</label>
  <input type="text" id="lname" name="lname">
  <input type="submit" value="Submit">
</form>

				
			

Output:

The <label> Element

The <label> element specifies a label for many form elements.

The <label> element is used by screen-reader users, because the screen-reader read out loud the label, while using the input element.

The <label> element also help users who face difficulty clicking on very small regions like the radio buttons or checkboxes.


The <select> Element

The <select> element specifies a drop-down list:

The elements provide an option that can be selected.

The first item in the drop-down list is selected by default.

Example

				
					<form action="/action_page.php">
  <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>
  <input type="submit">
</form>


				
			

Output:

Visible Values:

Using the size attribute specifies the number of visible values:

				
					<select id="cars" name="cars" size="3">
   

				
			

Allow Multiple Selections:

Using the multiple attribute enables the user to select multiple value:

				
					  <select id="cars" name="cars" size="4" multiple>
				
			

The <textarea> Element

The <textarea> element specifies a multi-line input field , that is a text area:

Example

				
					<form action="/action_page.php">
  <textarea name="message" rows="5" cols="30">The cat was playing in the garden.</textarea>
  <input type="submit">
</form>

				
			

Output:

The <button> Element

The <button> element is a clickable button:

Example

				
					<button type="button" onclick="alert('Hello World!')">Click Me!</button>
				
			

Output:

The <fieldset> and <legend> Elements

The <fieldset> element groups related data in a form.

The <legend> element captions the <fieldset> element.

Example

				
					<form action="/action_page.php">
  <fieldset>
    <legend>Personalia:</legend>
    <label for="fname">First name:</label><br>
    <input type="text" id="fname" name="fname" value="John"><br>
    <label for="lname">Last name:</label><br>
    <input type="text" id="lname" name="lname" value="Doe"><br><br>
    <input type="submit" value="Submit">
  </fieldset>
</form>

				
			

Output:

Personalia:

The <datalist> Element

The <datalist> element consists of a list of pre-defined options for an <input> element.

The list attribute of the <input> element,indicates to the id attribute of the <datalist> element.

Example

				
					<form action="/action_page.php">
  <input list="browsers" name="browser">
  <datalist id="browsers">
    <option value="Internet Explorer">
    <option value="Firefox">
    <option value="Chrome">
    <option value="Opera">
    <option value="Safari">
  </datalist>
  <input type="submit">
</form>

				
			

Output:

The <output> Element

The <output> element displays the result of a calculation,

Example

				
					<form action="/action_page.php"
oninput="x.value=parseInt(a.value)+parseInt(b.value)">
  0
  <input type="range" id="a" name="a" value="50">
  100 +
  <input type="number" id="b" name="b" value="50">
  =
  <output name="x" for="a b"></output>
  <br><br>
  <input type="submit">
</form>

				
			

Output:

0 100 + =