HTML for Web Development

0% completed

Previous
Next
Form Controls (Input Elements)

In this lesson, you'll learn about the various input elements available in HTML for collecting user data. We'll cover a range of input types provide examples for some of the most important controls.

Overview of Input Elements

HTML provides many input types that allow users to enter different kinds of data. Each input type serves a specific purpose and helps validate user input before it’s sent to the server.

Below is a summary table for common input types:

Input TypeDescription
textA single-line text field.
passwordA single-line field that masks user input.
emailValidates input as an email address.
urlValidates input as a URL.
searchA search field, similar to text but with specialized UI.
numberAccepts numerical values, optionally with min/max and step.
rangeA slider for selecting a value within a range.
dateA date picker for selecting a date.
timeA time picker for selecting a time.
datetime-localA combined date and time picker (without timezone).
checkboxA toggle control for binary choices (yes/no).
radioA group of options where only one can be selected.
fileAllows users to select and upload files.
hiddenAn invisible field for passing data with the form.
submitA button that submits the form.
resetA button that resets all form fields to their initial values.
buttonA general button that can be programmed for custom actions.

Text-Based Inputs

1. Text Input (text)

A plain, single-line text field for user input.

Example:

<input type="text" id="username" name="username" placeholder="Enter your username">

2. Password Input (password)

A text field that masks the input for sensitive information.

Example:

<input type="password" id="password" name="password" placeholder="Enter your password">

3. Email Input (email)

A field for entering email addresses with basic browser validation.

Example:

<input type="email" id="email" name="email" placeholder="you@example.com">

4. URL Input (url)

A field for entering website addresses.

Example:

<input type="url" id="website" name="website" placeholder="https://example.com">

5. Search Input (search)

Designed for search queries with some browsers offering extra UI features.

Example:

<input type="search" id="search" name="search" placeholder="Search our site">

Numeric Inputs

1. Number Input (number)

A field to enter numeric values, which can enforce minimum, maximum, and step values.

Example:

<input type="number" id="quantity" name="quantity" min="1" max="100" step="1">

2. Range Input (range)

Displays a slider to choose a value within a defined range.

Example:

<input type="range" id="volume" name="volume" min="0" max="100">

Date and Time Inputs

1. Date Input (date)

Provides a date picker so the user can select a calendar date.

Example:

<input type="date" id="birthday" name="birthday">

2. Time Input (time)

Allows the user to select a time of day.

Example:

<input type="time" id="appt" name="appt">

3. Datetime-Local Input (datetime-local)

Enables selection of both date and time without the timezone.

Example:

<input type="datetime-local" id="meeting" name="meeting">

Selection Inputs

1. Checkbox (checkbox)

A box that can be checked or unchecked, ideal for multiple selections.

Example:

<label> <input type="checkbox" name="subscribe" value="newsletter"> Subscribe to Newsletter </label>

2. Radio Buttons (radio)

A group of options where only one can be selected at a time.

Example:

<p>Gender:</p> <label> <input type="radio" name="gender" value="male"> Male </label> <label> <input type="radio" name="gender" value="female"> Female </label> <label> <input type="radio" name="gender" value="other"> Other </label>

3. Dropdown List (select and option)

A list of options from which a user can select one value.

Example:

<label for="country">Country:</label> <select id="country" name="country"> <option value="usa">USA</option> <option value="canada">Canada</option> <option value="uk">UK</option> </select>

4. Data List (datalist)

Provides an autocomplete feature for an <input> element.

Example:

<label for="browser">Choose a browser:</label> <input list="browsers" id="browser" name="browser"> <datalist id="browsers"> <option value="Chrome"> <option value="Firefox"> <option value="Safari"> <option value="Edge"> </datalist>

File Input

1. File Upload (file)

Allows users to select files from their device for upload.

Example:

<label for="resume">Upload your resume:</label> <input type="file" id="resume" name="resume">
  • Note: To allow multiple files, add the multiple attribute.

Hidden Input

1. Hidden Field (hidden)

Stores data that the user does not directly interact with.

Example:

<input type="hidden" name="form_id" value="12345">

Buttons

1. Submit Button (submit)

Sends the form data to the server.

Example:

<input type="submit" value="Submit">

2. Reset Button (reset)

Resets all form fields to their initial values.

Example:

<input type="reset" value="Reset">

3. Generic Button (button)

A versatile button for custom actions, which can be programmed with JavaScript.

Example:

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

Example: Putting All Input Elements Together

Below eample demonstrates how various form controls work together to collect user data.

HTML

. . . .

Explanation

  • Text Inputs:
    Collect first and last names using <input type="text">.

  • Email and Password:
    Use <input type="email"> for email (with basic validation) and <input type="password"> to mask the password.

  • Radio Buttons:
    Allow users to choose their gender. Only one option can be selected since they share the same name attribute.

  • Dropdown List:
    Users select their country from a predefined list using a <select> element with <option> tags.

  • Checkbox:
    Provides an option to subscribe to the newsletter. Multiple checkboxes can be used for multiple selections.

  • File Upload:
    <input type="file"> lets users upload a profile picture.

  • Hidden Field:
    <input type="hidden"> passes additional data (like a form identifier) without user interaction.

  • Submit and Reset Buttons:
    <input type="submit"> sends the form data to the server and <input type="reset"> clears all inputs.

Each input type is designed to capture specific data and enhance the overall user experience while interacting with forms. This comprehensive overview will serve as a solid foundation as we move on to the next lessons covering labels, grouping, validation, and advanced attributes.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next