I would like to add placeholder texture to select input using ReactJS, however my current attempt is not working.
My code looks like this:
<Input type="select" placeholder="placeholder">
<option>First option</option>
<option>second option</option>
<option>Thrid option</option>
</Input>
I am expecting the text "placeholder" to show up on the input field before any option has been selected, however the "first option" is showing by default.
I would like to add placeholder texture to select input using ReactJS, however my current attempt is not working.
My code looks like this:
<Input type="select" placeholder="placeholder">
<option>First option</option>
<option>second option</option>
<option>Thrid option</option>
</Input>
I am expecting the text "placeholder" to show up on the input field before any option has been selected, however the "first option" is showing by default.
Share Improve this question edited Feb 12, 2019 at 19:40 Dacre Denny 30.4k5 gold badges51 silver badges66 bronze badges asked Feb 12, 2019 at 19:18 user10398929user10398929 2 |2 Answers
Reset to default 19To achieve the placeholder effect on a <select>
element in ReactJS, first add the defaultValue
prop to the <select>
element and preassign a default value of say the empty string. Then, add a disabled <option>
item with a value that matches defaultValue
as shown:
<select defaultValue="">
<option disabled={true} value="">placeholder</option>
<option>First option</option>
<option>second option</option>
<option>Thrid option</option>
</select>
Update
A more robust approach is as follows:
<select defaultValue="">
<option hidden value="">placeholder</option>
<option>First option</option>
<option>second option</option>
<option>Thrid option</option>
</select>
You can achieve this functionality using
<option value="" hidden>
Your Placeholder here
</option>
Input
come from? – Matthew Herbst Commented Feb 12, 2019 at 19:22