Reputation: 604
I am working with html select boxes and have found that they currently don't support the placeholder attribute, but I don't exactly understand why this is the case.
I would just like to understand what are the reasons for this if any? Thanks for any insight.
Upvotes: 2
Views: 1012
Reputation: 3254
Placeholder text will appear when the user didn't feed any value.
selectbox, in this scenario the 1st option will appear when the user didn't enter a value. So we can use 1st <option>
as placeholder text & place holder attribute is not required.
Upvotes: 1
Reputation: 724252
Likely, this is because conventional placeholder text doesn't make sense in a select element because you only have a predefined set of options to choose from. You can't edit the text in a select element like you can with an editable combo box in many desktop applications, but that's only because HTML simply does not provide an editable select element out of the box.
The select element implements "placeholders" by way of what the HTML5 spec calls a placeholder label option, which is described thusly:
If a
select
element has arequired
attribute specified, does not have amultiple
attribute specified, and has a display size of 1; and if the value of the firstoption
element in theselect
element's list of options (if any) is the empty string, and thatoption
element's parent node is theselect
element (and not anoptgroup
element), then thatoption
is theselect
element's placeholder label option.
And it gives the following example:
Code Example:
The following example shows how a
select
element can be used to offer the user with a set of options from which the user can select a single option. The default option is preselected.<p> <label for="unittype">Select unit type:</label> <select id="unittype" name="unittype"> <option value="1"> Miner </option> <option value="2"> Puffer </option> <option value="3" selected> Snipey </option> <option value="4"> Max </option> <option value="5"> Firebot </option> </select> </p>
When there is no default option, a placeholder can be used instead:
<select name="unittype" required> <option value=""> Select unit type </option> <option value="1"> Miner </option> <option value="2"> Puffer </option> <option value="3"> Snipey </option> <option value="4"> Max </option> <option value="5"> Firebot </option> </select>
This idiom has in fact been in use since antiquity.
Upvotes: 3