0% completed
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.
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 Type | Description |
---|---|
text | A single-line text field. |
password | A single-line field that masks user input. |
email | Validates input as an email address. |
url | Validates input as a URL. |
search | A search field, similar to text but with specialized UI. |
number | Accepts numerical values, optionally with min/max and step. |
range | A slider for selecting a value within a range. |
date | A date picker for selecting a date. |
time | A time picker for selecting a time. |
datetime-local | A combined date and time picker (without timezone). |
checkbox | A toggle control for binary choices (yes/no). |
radio | A group of options where only one can be selected. |
file | Allows users to select and upload files. |
hidden | An invisible field for passing data with the form. |
submit | A button that submits the form. |
reset | A button that resets all form fields to their initial values. |
button | A general button that can be programmed for custom actions. |
A plain, single-line text field for user input.
Example:
<input type="text" id="username" name="username" placeholder="Enter your username">
A text field that masks the input for sensitive information.
Example:
<input type="password" id="password" name="password" placeholder="Enter your password">
A field for entering email addresses with basic browser validation.
Example:
<input type="email" id="email" name="email" placeholder="you@example.com">
A field for entering website addresses.
Example:
<input type="url" id="website" name="website" placeholder="https://example.com">
Designed for search queries with some browsers offering extra UI features.
Example:
<input type="search" id="search" name="search" placeholder="Search our site">
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">
Displays a slider to choose a value within a defined range.
Example:
<input type="range" id="volume" name="volume" min="0" max="100">
Provides a date picker so the user can select a calendar date.
Example:
<input type="date" id="birthday" name="birthday">
Allows the user to select a time of day.
Example:
<input type="time" id="appt" name="appt">
Enables selection of both date and time without the timezone.
Example:
<input type="datetime-local" id="meeting" name="meeting">
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>
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>
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>
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>
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">
multiple
attribute.Stores data that the user does not directly interact with.
Example:
<input type="hidden" name="form_id" value="12345">
Sends the form data to the server.
Example:
<input type="submit" value="Submit">
Resets all form fields to their initial values.
Example:
<input type="reset" value="Reset">
A versatile button for custom actions, which can be programmed with JavaScript.
Example:
<button type="button" onclick="alert('Hello!')">Click Me</button>
Below eample demonstrates how various form controls work together to collect user data.
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.
.....
.....
.....