Setting select value to null or undefined does not change to placeholder-like option
See original GitHub issueMaybe this is just an ugly pattern of the web, but it’s pretty typical to have the first option of a select be a sort of placeholder like item.
<select>
<option value=''>Select a thing</option>
....more options
</select>
When implemented with react, I would expect if i set the prop value
of the select to null || undefined
it would change to the placeholder-like option. But it seems you have to pass an empty string value to pull it off. I’m not sure if this is something React should mask/handle or not. It seems more like an oddity of the browser… So I am suggesting one of two things:
- Have
null || undefined
value props choose an empty value option. invariant
warn passingnull || undefined
is non-effective.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:5
- Comments:8 (3 by maintainers)
Top Results From Across the Web
select option with default value undefined with required ...
To set a placeholder value of your select, set the state of the model to null and add an option with a null...
Read more >PDOStatement::bindParam - Manual - PHP
Binds a PHP variable to a corresponding named or question mark placeholder in the SQL statement that was used to prepare the statement....
Read more >DBI - Database independent interface for Perl - metacpan.org
undef NULL values are represented by undefined values in Perl ... Binding an undef (NULL) to the placeholder will not select rows which...
Read more >Placeholder for required <select>s by Taylor Hunt on CodePen
If the first <option> of a required <select> has its value set to the empty string, it becomes a placeholder. Don't believe me?...
Read more >ddSlick - a jQuery plugin for custom drop down with images
Allows JSON to populate the drop down options. Converts your HTML select element to ddSlick. Uses Minimum css and no external stylesheets to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
05 10 1995
If someone pass by, here what I do to have a select “placeholder”:
If
month
isnull
orundefined
=>value
is''
(empty string).You could also write:
value={this.props.month ? this.props.month : '0'}
+<option value="0" disabled>Month</option>
value={'' + this.props.month}
+<option value="undefined">Month</option>
But I don’t recommend it and you should stay with
<option value="" ...>
instead because HTML5 validation with attributerequired
only works if the value of the first option element in the select element’s list of options (if any) is the empty string.Visually:
Since
Month
isdisabled
, the user cannot select it. If you don’t wantMonth
to be displayed in the list, used thehidden
attribute (works perfectly under Chrome 54 and Firefox 47; not/partially under IE 11, Edge 38 and Safari 10).