Advanced HTML Form Controls

Tutorial 5 of 5

Advanced HTML Form Controls

1. Introduction

This tutorial aims to provide an in-depth understanding of advanced HTML form controls. By the end of this tutorial, you will be able to create interactive and user-friendly web forms using advanced form controls.

What You Will Learn

  • How to use advanced form controls such as datalist, optgroup, output, and progress
  • Best practices for using advanced form controls

Prerequisites

Basic knowledge of HTML forms and form controls is required. Familiarity with HTML tags and attributes is also beneficial.

2. Step-by-Step Guide

Datalist

The <datalist> element provides an "autocomplete" feature on form elements. It contains a set of <option> elements that represent the permissible or recommended options available to choose from within other controls.

<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>

Optgroup

The <optgroup> tag is used to group related options in a drop-down list. If you have a long list of options, groups of related options are easier to handle for a user.

<select>
  <optgroup label="Swedish Cars">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
  </optgroup>
  <optgroup label="German Cars">
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
  </optgroup>
</select>

Output

The <output> tag represents the result of a calculation (like one performed by a script).

<form oninput="result.value=parseInt(a.value)+parseInt(b.value)">
  <input type="range" id="a" value="50">+
  <input type="number" id="b" value="50">=
  <output name="result" for="a b">100</output>
</form>

Progress

The <progress> tag represents the progress of a task.

<progress value="22" max="100"></progress>

3. Code Examples

Example 1: Using <datalist>

<!-- Here, an input field is linked to a datalist via the list attribute. -->
<input list="cars" name="car">
<datalist id="cars">
  <!-- The options inside the datalist are shown as suggestions in the input field. -->
  <option value="Audi">
  <option value="BMW">
  <option value="Mercedes">
  <option value="Volvo">
</datalist>

Example 2: Using <optgroup>

<!-- The optgroup tag is used to categorize options in a select box. -->
<select>
  <optgroup label="Fruits">
    <!-- Options under the 'Fruits' category -->
    <option value="apple">Apple</option>
    <option value="banana">Banana</option>
  </optgroup>
  <optgroup label="Vegetables">
    <!-- Options under the 'Vegetables' category -->
    <option value="carrot">Carrot</option>
    <option value="celery">Celery</option>
  </optgroup>
</select>

4. Summary

In this tutorial, we have learned about advanced form controls in HTML including <datalist>, <optgroup>, <output>, and <progress>.

To further your learning, consider exploring other advanced form controls such as <meter> and <fieldset>. Also, familiarize yourself with form validation and handling form data.

5. Practice Exercises

Exercise 1: Create an HTML form with a <datalist> of your five favorite books.

Exercise 2: Create a dropdown list using <optgroup> for categories of food items.

Solutions: You can find the solutions and explanations for these exercises on W3Schools.

Remember, practice is key when learning web development, so keep experimenting and building with these form controls.