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.

The selected attribute for <option> tags are not rendered correctly

See original GitHub issue

This 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:closed
  • Created 8 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

8reactions
gaearoncommented, May 2, 2016

Your first example is missing the value attribute on the option. From the documentation, this is the correct usage:

  <select value="B">
    <option value="A">Apple</option>
    <option value="B">Banana</option>
    <option value="C">Cranberry</option>
  </select>

Otherwise React thinks none of them have values, so it can’t match your select’s value to any of the options.

3reactions
StJohn3Dcommented, Apr 28, 2016

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

Read more comments on GitHub >

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

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