Select, Option & Dropdowns
By the end of this lesson you will be able to build a dropdown that sends clean data, forces a real choice, groups a long list and stays easy to use. You will also learn when a dropdown is the wrong tool.
You met <select> briefly in Datalist & Output Elements. This lesson goes deeper, including the traps that catch even experienced developers.
How a dropdown is built
Think of the menu card at a dhaba. You point at "Paneer butter masala", but the waiter writes "PBM-2" on the order slip. The name is for you; the code is for the kitchen. A dropdown works the same way.
<select>is the box. It carries thenamethat the server receives.<option>is one choice. Itsvalueis sent, and its text is shown.
Step 1. Write a label, a select and three options.
<label for="city">Nearest centre</label>
<select id="city" name="city">
<option value="JAI">Jaipur</option>
<option value="PNQ">Pune</option>
<option value="COK">Kochi</option>
</select>You should see a closed box showing "Jaipur". Click it and the list opens. If the user picks Pune, the form sends city=PNQ. The for and id pair connects the label, exactly as you learned in Labels & Placeholders.
Value versus text
The value attribute and the visible text are two separate things. If you leave value out, the browser sends the visible text instead, after trimming extra spaces.
So why write a value at all? Because text changes and codes should not. Next month someone may translate "AC 3 Tier" into Hindi. If the server checks the exact text, the form breaks. A fixed value such as 3A keeps working.
Step 2. Run this railway class picker. It shows the value and the text side by side.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Travel Class</title>
<style>
body { font-family: system-ui, sans-serif; margin: 24px; }
select { font-size: 16px; padding: 4px; }
</style>
</head>
<body>
<label for="travel-class">Travel class</label>
<select id="travel-class" name="class">
<option value="SL">Sleeper</option>
<option value="3A" selected>AC 3 Tier</option>
<option value="2A">AC 2 Tier</option>
<option value="1A">AC First Class</option>
<option>General</option>
</select>
<p>Sent to the server: <output id="sent"></output></p>
<p>Shown to the user: <output id="shown"></output></p>
<script>
const box = document.querySelector("#travel-class");
function show() {
document.querySelector("#sent").textContent = "class=" + box.value;
document.querySelector("#shown").textContent = box.selectedOptions[0].text;
}
box.addEventListener("change", show);
show();
</script>
</body>
</html>You should see "class=3A" and "AC 3 Tier". Now choose "General": it has no value, so its text is sent. In the script, box.value reads the value and selectedOptions[0].text reads the text.
Choosing the starting option with selected
A single dropdown always shows one option. Without any selected attribute, the browser picks the first option that is not disabled. Add selected to a different option to change that, as the example above does with "AC 3 Tier".
selected is most useful on edit pages. When Ananya opens her saved profile, the server writes selected on the state she chose last time. Put it on only one option in a normal dropdown.
Grouping options with optgroup
A long list is easier to scan in groups. Wrap related options in <optgroup> with a label. The label appears as a heading that users cannot select. Groups cannot be nested, and disabled on a group disables every option in it.
Forcing a real choice with required
Sometimes no option is a safe default; a blood group must never be guessed. The fix has two parts:
- Make the first option a prompt with an empty value, such as
<option value="">Choose a course</option>. - Add
requiredto the<select>.
Now the form will not submit while the prompt is selected. The prompt must be a direct child of <select>, not inside an <optgroup>. The HTML standard requires this prompt on every required one-row dropdown, and the W3C validator reports it when missing.
Disabled options
A disabled option stays visible but greyed out. Nobody can pick it, and it is never sent. Use it for something that exists but is not available now, such as a full batch. Users see it and understand why they cannot book it.
Step 3. This admission form uses everything so far: a required prompt, groups and a disabled option.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Admission Enquiry</title>
<style>
body { font-family: system-ui, sans-serif; margin: 24px; }
select, button { font-size: 16px; margin-top: 8px; }
</style>
</head>
<body>
<h1>Lucknow Coding Club: admission enquiry</h1>
<form action="#" id="enquiry">
<label for="course">Course</label><br>
<select id="course" name="course" required>
<option value="">Choose a course</option>
<optgroup label="Beginner">
<option value="html">HTML and CSS</option>
<option value="py">Python basics</option>
</optgroup>
<optgroup label="Advanced">
<option value="react" disabled>React (batch full)</option>
<option value="node">Node.js</option>
</optgroup>
</select>
<br>
<button type="submit">Send enquiry</button>
</form>
<p>Form data: <output id="result">nothing sent yet</output></p>
<script>
const form = document.querySelector("#enquiry");
form.addEventListener("submit", (event) => {
event.preventDefault(); // demo only: show the data instead of sending it
const data = new URLSearchParams(new FormData(form));
document.querySelector("#result").textContent = data.toString();
});
</script>
</body>
</html>Press "Send enquiry" straight away: the browser stops you. React cannot be picked. Choose Node.js, submit, and you should see course=node.
Picking several options: multiple and size
The boolean multiple attribute lets users select more than one option. The box changes from a dropdown into a list box that shows four rows by default. The size attribute sets how many rows are visible. A size above 1 on a normal select also turns it into a list box, but it still allows only one choice.
<label for="toppings">Pizza toppings</label>
<select id="toppings" name="toppings" multiple size="5">
<option value="paneer">Paneer</option>
<option value="onion">Onion</option>
<option value="capsicum">Capsicum</option>
<option value="corn">Sweet corn</option>
<option value="olive">Olives</option>
</select>If Rohan picks paneer and onion, the form repeats the name once for each choice:
toppings=paneer&toppings=onionThe problem with multiple is usability. Users must hold Ctrl (or Cmd on a Mac) to pick a second item, and one careless click clears every choice. A group of checkboxes, from Radio Buttons & Checkboxes, is almost always easier.
Dropdown or radio buttons?
A dropdown hides its choices until it is opened. That saves space but costs the user a click. Use this guide:
| Situation | Better choice | Why |
|---|---|---|
| 2 to 5 options, such as UPI, card or cash | Radio buttons | All choices visible, one tap |
| About 6 to 20 options, such as a state | <select> | Saves space, list is easy to scan |
| Hundreds of options, such as every city in India | Input with <datalist> or a search box | Scrolling a huge list is slow |
| Several answers allowed | Checkboxes | Clearer than multiple |
| Yes or No | Radio buttons or one checkbox | A dropdown hides a two-item choice |
Common mistakes
- Leaving out
valueand relying on text. The form breaks when someone edits the wording. - A
requiredselect whose first option is a real choice. The first option is already selected, sorequirednever fails. Add an empty prompt option. - Putting
disabledon the prompt withoutselected. The browser skips disabled options when choosing the default, so the first real option gets selected silently.
Interview questions
1. What is the difference between an option's value and its text?
The text is what the user sees; the value attribute is what the form sends. If value is missing, the trimmed text is sent, so a stable value is safer when wording may change.
2. How do you make sure a user actually picks an option from a select?
Add an empty-value prompt as the first option, directly inside <select>, and add required. While the prompt is selected the value is empty, so the browser blocks submission.
3. What changes when you add the multiple attribute?
The control becomes a list box allowing several selections, showing four rows unless size says otherwise. The name is sent once per selected option. Because users must Ctrl-click, checkboxes are often better.
4. When would you choose radio buttons instead of a select?
When there are about five options or fewer, because radios show every choice at once and need one tap. A select suits longer lists where saving space matters more.
Key points
<select>carries thename; each<option>carries avalue(sent) and text (shown).- Without a
value, the option's text is sent, so set stable values. selectedsets the starting option;<optgroup label="...">groups options.- An empty first option plus
requiredforces a real choice. multipleandsizeturn the dropdown into a list box; checkboxes are usually easier.- Use radio buttons for a few options and a datalist or search for very long lists.
Exercise
Build a "Book a cricket net" form for a Rohtak sports club. Add a required time-slot dropdown with a "Choose a slot" prompt and two groups: "Morning" (6 am, 7 am) and "Evening" (5 pm, 6 pm), with 6 pm disabled because it is booked. Add an optional bowling-speed dropdown with "Medium" selected, and a submit button.
Show solution
<form action="#">
<label for="slot">Time slot</label>
<select id="slot" name="slot" required>
<option value="">Choose a slot</option>
<optgroup label="Morning">
<option value="0600">6 am</option>
<option value="0700">7 am</option>
</optgroup>
<optgroup label="Evening">
<option value="1700">5 pm</option>
<option value="1800" disabled>6 pm (booked)</option>
</optgroup>
</select>
<label for="speed">Bowling machine speed</label>
<select id="speed" name="speed">
<option value="slow">Slow</option>
<option value="medium" selected>Medium</option>
<option value="fast">Fast</option>
</select>
<button type="submit">Book net</button>
</form>The prompt sits directly inside <select> with an empty value, so required works. The booked slot is visible but cannot be chosen.
Quick Quiz
1. An option is written as <option>Kochi</option> with no value. What is sent if it is chosen?
When value is missing, the option's text is used as its value.
2. Why does required not work on a select whose first option is "Delhi"?
The first option is selected by default; only an empty-value prompt lets required fail.
3. Which attribute shows a select as a list with six visible rows?
size sets the number of visible rows; rows belongs to <textarea>.
4. A payment form offers UPI, card and cash on delivery. What is the best control?
With only three choices, radio buttons show all options at once and need one tap.
Learn HTML with a mentor
Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
