question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting select value to null or undefined does not change to placeholder-like option

See original GitHub issue

Maybe 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:

  1. Have null || undefined value props choose an empty value option.
  2. invariant warn passing null || undefined is non-effective.

jsbin example

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:5
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

22reactions
mahadehassan34commented, Nov 25, 2016

05 10 1995

17reactions
tkrotoffcommented, Sep 18, 2017

If someone pass by, here what I do to have a select “placeholder”:

<select name="month"
        value={this.props.month ? this.props.month : ''}
        onChange={handleChange}>
  <option value="" disabled>Month</option>
  <option value="1">Jan</option>
  <option value="2">Feb</option>
  <option value="3">Mar</option>
  <option value="4">Apr</option>
  <option value="5">May</option>
  <option value="6">Jun</option>
  <option value="7">Jul</option>
  <option value="8">Aug</option>
  <option value="9">Sep</option>
  <option value="10">Oct</option>
  <option value="11">Nov</option>
  <option value="12">Dec</option>
</select>

If month is null or undefined => value is '' (empty string).

You could also write:

  • value={this.props.month ? this.props.month : '0'} + <option value="0" disabled>Month</option>
  • or 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 attribute required 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:

screen shot 2016-11-25 at 17 07 28

screen shot 2016-11-25 at 17 07 40

Since Month is disabled, the user cannot select it. If you don’t want Month to be displayed in the list, used the hidden attribute (works perfectly under Chrome 54 and Firefox 47; not/partially under IE 11, Edge 38 and Safari 10).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found