Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialdotz
6,733 Pointsoption value purpose
From MDN: option value: The content of this attribute represents the value to be submitted with the form, should this option be selected. If this attribute is omitted, the value is taken from the text content of the option element.
Is it just good practice to include the value even when it mirrors the text field (as Nick's does)? Or is there a reason to include value with option? Seems like a lot of unneccesary extra typing.
1 Answer
miguelcastro2
Courses Plus Student 6,573 PointsLet's say you have a drop down menu on an employee form and you want this drop down to represent the employee's status. It could contain the statuses "Applied", "Hired", and "Terminated". In the employee database we store the status as a number which is more efficient, so applied would equal 1, hired equal 2, and terminated equal 3. Now, if we wanted to display the status of an employee in the drop down, but when we save we only want to use our values then the drop down may look like this:
<select>
<option value="1">Applied</option>
<option value="2">Hired</option>
<option value="3">Terminated</option>
</select>
If we stored our statuses as text then we could omit the value attribute. So in a nutshell, the value attribute allows us to assign values to our options that are different then their displayed values.
dotz
6,733 Pointsdotz
6,733 PointsThank you!
pat barosy
6,759 Pointspat barosy
6,759 PointsGreat example. Thank you