The selected attribute for <option> tags are not rendered correctly
See original GitHub issueThis will cause Selenium tests to fail when using getFirstSelectedOption() to find the selected item with the Java API. This seems to affect at least 0.12 and forward. This example should set the selected attribute on the selected option-tag.
import React from 'react';
import ReactDOM from 'react-dom';
var App = React.createClass({
getInitialState: function() {
return {
value: "a"
};
},
onChange: function (event) {
this.setState({value: event.target.value});
},
render: function () {
return <div>
<select value={this.state.value} onChange={this.onChange}>
<option value="a">a</option>
<option value="b">b</option>
</select>
</div>;
}
});
ReactDOM.render(<App/>, document.getElementById("app"));
Issue Analytics
- State:
- Created 8 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
HTML selected="selected" not working properly - Stack Overflow
Ctrl-r, selected option tag is not shown as selected but your last gui selection is. Hit return on the page address field in...
Read more >The HTML Option element - MDN Web Docs - Mozilla
If present, this Boolean attribute indicates that the option is initially selected. If the <option> element is the descendant of a <select> ...
Read more >Multiple and selected attribute not displaying in browsers
I'm using the "select" tag with the "multiple" attribute included. With the "multiple" attribute included in the "select" tag, "selected" ...
Read more >Forms in HTML documents - W3C
When rendering a menu choice, user agents should use the value of the label attribute of the OPTION element as the choice. If...
Read more >How to Use Option Selected Property in JavaScript - Tabnine
In the above code, the HTML document has a <select> element. Its id is set to greet, and it has three options 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
Your first example is missing the
value
attribute on theoption
. From the documentation, this is the correct usage:Otherwise React thinks none of them have
value
s, so it can’t match yourselect
’svalue
to any of theoption
s.I’m having the same issue. On change will get the correct event information but the select element on the dom still reflects the initial value. Only on the second update (with the same selection - no change) will the select element reflect the true current selection.
The only way I was able to fix this was to use the selected property on
<option>
elements during my mapping function…<option> selected={opVal === props.selectedVal} key={index}>{opVal}</option>
This of course gets me React warnings saying to use the value prop on
<Select>
but since that’s not working I’m just living with the warnings.Please fix this