Think of filling out a sign-up page. Clear fields make the job simple; confusing fields make people leave.
What You Need to Know
Two independent checkboxes allow different answers for analytics and newsletters.
Try the Example
Take a look at the example. We’ll explain the important lines right below it.
<form action="/privacy" method="post">
<fieldset>
<legend>Privacy choices</legend>
<label><input name="analytics" type="checkbox" value="yes"> Allow analytics</label>
<label><input name="news" type="checkbox" value="yes"> Receive news</label>
</fieldset>
<button type="submit">Save choices</button>
</form>
Let’s Read the Code
- <form>: The form groups controls and identifies where submitted data goes.
- <fieldset>: The fieldset keeps related choices together.
- name: The name is the key used for the submitted value.
- type: Type changes what sort of control or resource the browser uses.
Read the named elements and attributes together. They explain how the browser connects this feature to the rest of the page.
What Happens in the Browser?
The HTML controls work in the browser, but saving an answer requires the example action path to be handled by a real server.
Where does the answer go? The example uses the submitted name(s) analytics, news. Those names help the receiving server identify each answer. A label is for people; a name attribute is for the form data; an ID connects elements in the page.
Your Turn
Uncheck both boxes and list the names that are absent from the submitted request.
Common Beginner Mistakes
- Copying the example without giving the fields a clear label and a usable name.
- A missing checkbox key means unchecked, not necessarily an explicit no; the server should store that meaning correctly.
Key Takeaways
- A control needs a clear label and, when submitted, a meaningful name and value.
- <form>: The form groups controls and identifies where submitted data goes.
- A meaningful HTML element is usually a stronger starting point than adding an ARIA role to a generic div. Also check what happens using only the keyboard.