HTML <option> Tag โ Dropdown Item
๐ Introduction
The <option> tag is used to define items inside a <select> dropdown list.
Each <option> represents a single choice the user can select.
๐ It only works inside <select>.
๐งฑ Syntax
<select>
<option>Option 1</option>
<option>Option 2</option>
</select>
๐ก Example
<!DOCTYPE html>
<html>
<head>
<title>Option Example</title>
</head>
<body>
<h2>Select a Language</h2>
<form>
<select name="language">
<option>HTML</option>
<option>CSS</option>
<option>JavaScript</option>
</select>
<br /><br />
<button type="submit">Submit</button>
</form>
</body>
</html>
๐ฅ๏ธ Output Explanation
- Each
<option>becomes a dropdown item - User selects one value
- Only one option is active (by default)
๐ฏ Important Attributes
1. value
<option value="html">HTML</option>
โ Value sent to server when form is submitted
2. selected
<option selected>HTML</option>
โ Makes an option selected by default
3. disabled
<option disabled>Coming Soon</option>
โ Makes option unselectable
๐ง Important Concept
๐ <option> = single choice inside dropdown
๐ <select> = container for all options
๐งฉ Example with Values
<select name="country">
<option value="dz">Algeria</option>
<option value="ma">Morocco</option>
<option value="tn">Tunisia</option>
</select>
๐ฏ Why value is Important
Even if user sees text like:
Algeria
The server receives:
dz
โ This is useful for databases and APIs
๐จ Styling Example
<option style="color:red;">HTML</option>
๐ซ Common Mistakes
- Using
<option>outside<select>โ - Forgetting
valuein forms โ - Not using
namein<select>โ
๐งช Mini Exercise
Create a form with:
- A
<select>for choosing a programming language - At least 5
<option>items - One default selected option
- Submit button