Is it semantically incorrect to assign a class to <option>
tag or placeholder
on <select>
tag?
Are there any other downsides of it?
1 に答える
class
is a global attribute in HTML5 and thus can be applied to option
but placeholder
isn't allowed on select
. Why? Probably for practical/historical reasons as the first option is displayed in the select - of size
< 2 - and doesn't leave room for displaying another text as in an empty input[type="text"]
How could you then achieve the same effect as a placeholder in a text input? Well you don't need it:
(HTML5 doc on placeholder)
The placeholder attribute represents a short hint (a word or short phrase) intended to aid the user with data entry when the control has no value.
- Selecting an option doesn't need any data entry (a date does need a format indicated, like mm/dd/aaaa or dd/mm/aaaa...)
- There's already a label to indicate the purpose of the select (associated with
for/id
attributes on each) - What should you put in the first option? That's an excellent question... Between
-------
("oh but then the user may not see the select"),Please select blah
,(repeating the label)
andfirst option but we'll never know if user did choose this option or forgot to select another one
... that's an ergonomics problem and not my field of expertise. ;)