In Send the Form Without a Reload, you wrapped Sam’s boxes in a form. Now Enter and the Add button both send it, and the submit listener’s event.preventDefault() keeps the page in place. The listener still reads each box on its own, though, with a constant and a querySelector() call per box. That gets clumsy as soon as the form grows.
Sam wants to note which aisle each item is in and whether it’s needed today. In this lesson, you’ll collect the form’s entries in one object, predict what that object holds, and turn each submission into a record that render() draws. Start by giving the form something to read.
Name the Controls
A form identifies each value it sends by the control’s name attribute. The id is how your script and your CSS find one element. The name is the label the form files the control’s value under. Your boxes have no names yet, which is why the address in the last lesson ended in a bare ?.
In index.html, give the two boxes inside #add-form a name each:
kitchen/index.html
<label>
Item
<input id="item-name" name="name">
</label>
<label>
How many?
<input id="item-count" name="count" type="number">
</label>
Sam’s two new details need different kinds of control. You choose an aisle from a short, fixed list, so use a <select> element, which the browser draws as a dropdown list. Whether Sam needs the item today is a yes-or-no choice, so use a checkbox, an <input> with type="checkbox".
Add both after the How many? label, before the Add button:
kitchen/index.html
<label>
Aisle
<select name="aisle">
<option value="produce">Produce</option>
<option value="dairy">Dairy</option>
<option value="pantry">Pantry</option>
<option value="bakery">Bakery</option>
</select>
</label>
<label>
<input type="checkbox" name="today">
Need it today
</label>
Each <option> has two texts. Sam sees the words between the tags, such as Dairy, and your script reads the value attribute, such as "dairy". The first option stays selected until Sam picks another. Because the checkbox sits inside the <label>, clicking the words Need it today ticks it too. The box goes before its words, the usual order for a checkbox.
Save, and the form shows an Aisle list set to Produce and an unticked Need it today box.