Checkbox Input: type checkbox attribute & value attribute
HTML FORMS
So far the types of inputs we’ve allowed were all single choices. But, what if we presented multiple options to users and allow them to select any number of options? Sounds like we could use checkboxes! In a <form> we would use the <input> element and set type="checkbox". Examine the code used to create multiple checkboxes:
<form>
<p>Choose your pizza toppings:</p>
<label for="cheese">Extra cheese</label>
<input id="cheese" name="topping" type="checkbox" value="cheese">
<br>
<label for="pepperoni">Pepperoni</label>
<input id="pepperoni" name="topping" type="checkbox" value="pepperoni">
<br>
<label for="anchovy">Anchovy</label>
<input id="anchovy" name="topping" type="checkbox" value="anchovy">
</form>
Which renders:
Notice in the example provided:
- there are assigned values to the
valueattribute of the checkboxes. These values are not visible on the form itself, that’s why it is important that we use an associated<label>to identify the checkbox. - each
<input>has the same value for thenameattribute. Using the samenamefor each checkbox groups the<input>s together. However, each<input>has a uniqueidto pair with a<label>.
Alright, time to use checkboxes in our code!
Instructions:
1.Time to add some toppings! In the <section> with class="toppings", there are two <label>s but no associated <input> elements. Add an <input> element associated with the first <label>.
The created <input> should have:
- an
idset to"lettuce". - a
nameattribute with a value of"topping". - a
typeset to"checkbox" - a
valueof"lettuce". - Hint:
- In this case, add the
<input>before the<label>, so the text appears to the right of the checkbox. Assign the provided attributes inside the opening tag of the<input>.
<input> element and associate it with the second <label>.The <input> element should have:
- an
idset to"tomato". - a
typeset to"checkbox". - a
nameattribute with a value of"topping". - a
valueof"tomato".
Add another <input type="checkbox"> and <label> pair. Assign the name of the <input> to "topping". You’re free to decide the value and id but make sure that your new <label> and <input> are associated.
Hint:
You’ll need to add both a new <input> and an associated <label>.
The type of the <input> must be "checkbox" and the name set to "topping".
You’re free to decide the id, however, the value of the <label>‘s for attribute must match the id of the <input> element. The value of the <input> can be anything you want but it cannot be blank.
Comments
Post a Comment