A form should feel easy to use. The visitor sees a question, gives an answer, and needs to know what will happen next.
What You Need to Know
The address field is hidden initially. JavaScript must show it for home delivery and decide when to make it required.
Try the Example
Take a look at the example. We’ll explain the important lines right below it.
<fieldset>
<legend>Would you like home delivery?</legend>
<label><input type="radio" name="delivery" value="pickup" checked> Pickup</label>
<label><input type="radio" name="delivery" value="home"> Home delivery</label>
</fieldset>
<div id="home-address" hidden>
<label for="street">Street address</label>
<input id="street" name="street" autocomplete="street-address">
</div>
<p>JavaScript must reveal and manage the address field when home delivery is chosen.</p>
Let’s Read the Code
- for: For connects a label or output with a control by ID.
- <fieldset>: The fieldset keeps related choices together.
- <legend>: The legend names a fieldset question.
- 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) delivery, street. 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
Write down what should happen if the visitor changes from home delivery back to pickup.
Common Beginner Mistakes
- Copying the example without giving the fields a clear label and a usable name.
- Putting a required field inside a hidden section can stop form submission or confuse visitors.
Key Takeaways
- A control needs a clear label and, when submitted, a meaningful name and value.
- for: For connects a label or output with a control by ID.
- 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.