HTML forms: checkbox MCQs Quiz | Class 10
This quiz covers Class X Computer Applications (Code 165), Unit 4: Lab Exercises, focusing on HTML forms and multiple-choice checkbox inputs. Attempt all 10 multiple-choice questions and then click ‘Submit Quiz’ to see your score. You can also download a detailed answer PDF.
Understanding HTML Checkboxes for Multiple-Choice Input
HTML forms are essential for user interaction on websites, and the <input type="checkbox"> element is a fundamental component for allowing users to select one or more options from a given list. This section will delve into the details of using checkboxes effectively in web forms.
Key Concepts of Checkboxes
- Multiple Selection: Unlike radio buttons (
<input type="radio">) where only one option can be selected within a named group, checkboxes allow users to select zero, one, or many options from a list. - Boolean State: Each checkbox has a boolean state: it is either checked or unchecked. When a form is submitted, only the checkboxes that are checked (and have a
nameattribute) will have theirvaluesent to the server. nameAttribute: It is crucial to assign anameattribute to checkboxes. For a group of related checkboxes where multiple selections are possible, they should all share the samenameattribute (e.g.,<input type="checkbox" name="interests[]" value="sports">). This allows the server-side script to receive an array of selected values.valueAttribute: Thevalueattribute specifies the data to be sent to the server when the checkbox is checked. If novalueattribute is specified, the default value “on” is sent.checkedAttribute: This boolean attribute makes a checkbox pre-selected when the page loads.idand<label>: Always associate a<label>element with a checkbox using itsidattribute. This improves accessibility, allowing users to click the label text to toggle the checkbox, and screen readers to correctly announce the control.
Example Usage
Consider a scenario where a user selects their favorite programming languages:
<form action="/submit-preferences" method="post">
<p>Select your favorite programming languages:</p>
<input type="checkbox" id="langHTML" name="languages[]" value="HTML">
<label for="langHTML">HTML</label><br>
<input type="checkbox" id="langCSS" name="languages[]" value="CSS">
<label for="langCSS">CSS</label><br>
<input type="checkbox" id="langJS" name="languages[]" value="JavaScript" checked>
<label for="langJS">JavaScript</label><br>
<input type="checkbox" id="langPython" name="languages[]" value="Python">
<label for="langPython">Python</label><br>
<button type="submit">Submit</button>
</form>
In this example, if the user checks HTML and Python, and JavaScript was pre-checked, the server would receive `languages[] = [“HTML”, “JavaScript”, “Python”]`.
Difference from Radio Buttons
The primary distinction between checkboxes and radio buttons lies in the number of selections allowed:
| Feature | Checkboxes (type="checkbox") |
Radio Buttons (type="radio") |
|---|---|---|
| Selection Count | Zero, one, or multiple options can be selected. | Exactly one option must be selected within a named group. |
| Group Name | Multiple checkboxes can share the same name to submit an array of values. |
Multiple radio buttons must share the same name to form a mutually exclusive group. |
| Deselection | Can be easily deselected by clicking again. | Cannot be deselected once selected (another option must be chosen). |
Quick Revision Points
- Use
<input type="checkbox">for multiple selections. - Each checkbox needs a
nameandvalueattribute. - Use
checkedattribute for default selection. - Always pair with
<label>for accessibility. - Differentiate from radio buttons for single-choice scenarios.
Practice Questions
- Which HTML input type allows users to select multiple options from a list?
<input type="radio"><input type="text"><input type="checkbox"><input type="select">- What is the purpose of the
valueattribute in a checkbox? - To set the default checked state.
- To specify the text displayed next to the checkbox.
- To define the data sent to the server when the checkbox is checked.
- To group related checkboxes together.
- If no
valueattribute is specified for a checked checkbox, what value is sent to the server by default? - “true”
- “on”
- An empty string
- The checkbox’s
id - Which attribute is used to pre-select a checkbox when the page loads?
selecteddefaultactivechecked- Why is it good practice to associate a
<label>with an<input type="checkbox">? - It makes the checkbox visually larger.
- It allows styling the checkbox more easily.
- It improves accessibility and user experience.
- It is required for the checkbox to function correctly.