Text boxes are not always the best control. When the user must choose from a fixed set, or write a longer message, HTML offers better controls.
Radio Buttons and Checkboxes
A radio button lets the user pick one option from a group. All radio buttons in a group share the same name, and the browser lets only one of them be selected. A checkbox lets the user switch one option on or off, and several checkboxes can be selected together. The value attribute holds the text sent to the server, and the checked attribute selects an option at the start.
Definition: Radio button. A control that lets the user pick exactly one option from a group.
Definition: Checkbox. A control that lets the user switch one option on or off.
Drop-down Lists and Text Areas
The <select> element creates a drop-down list, and each choice is an <option> element. The multiple attribute lets the user pick more than one choice. The <textarea> element creates a box for several lines of text. Its size is set with the rows and cols attributes, and its content is written between its opening and closing tags.
Grouping Controls
A long form is easier to understand when related controls are grouped. The <fieldset> element draws a box around a group, and the <legend> element gives that box a title. Screen readers announce the legend before each control in the group.
Table 1.7: Choosing a form control
| Need | Control | Reason |
|---|---|---|
| One choice from few | Radio buttons | All options are visible |
| Many choices | Checkboxes | Each is independent |
| One choice from many | Select list | Saves space |
| Long text | Textarea | Accepts many lines |
Example 1.7: An admission form
<form>
<fieldset>
<legend>Course choice</legend>
Gender:
<input type="radio" name="g" value="F"> Female
<input type="radio" name="g" value="M"> Male<br><br>
Course:
<select name="course">
<option value="bca">BCA</option>
<option value="bba">BBA</option>
</select><br><br>
<input type="checkbox" name="hostel" checked> Need hostel<br><br>
<textarea name="note" rows="3" cols="30"></textarea>
</fieldset>
</form>
Output in the browser:
Choosing and Labelling Controls
Each control should have a visible label, so that the user knows what to enter. A radio button or checkbox is easier to hit when its label is linked, because clicking the text selects the control. A placeholder is only a hint and does not replace a label, since it disappears when the user types. HTML5 also gives specialised input types such as email, number, date and file. On a phone they open a suitable keyboard or picker, and browsers check their format automatically. Use the required attribute to mark the fields that must be filled in.
Common mistakes
- Giving radio buttons in one group different names, which lets the user select several.
- Leaving the value attribute out, so the server receives a vague or empty value.
- Using checkboxes when exactly one answer is allowed.
- Putting text for a textarea in a value attribute instead of between its tags.
Key takeaways
- Radio buttons give one choice, and checkboxes give many.
- A select element holds option elements, and textarea holds long text.
- Fieldset and legend group related controls.
- Every control needs a name, and every choice needs a clear value.
Practice questions
- What must radio buttons of one group share?
- Which element makes a drop-down list?
- What does legend do?
Answers to the practice questions
- The same name attribute.
- The select element, with option elements inside it.
- It gives a title to the box drawn by fieldset.
Sources
- Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit I
