OnChange event using React JS for drop down

var MySelect = React.createClass({ change: function(){ return document.querySelector('#lang').value; }, render: function(){ return( <div> <select> <option value="select" onChange={this.change}>Select</option> <option value="Java" onChange={this.change}>Java</option> <option value="C++" onChange={this.change}>C++</option> </select> <p></p> <p value={this.change}></p> </div> ); } }); React.render(<MySelect />, document.body); 

The onChange event does not work.

8 Answers

The change event is triggered on the <select> element, not the <option> element. However, that's not the only problem. The way you defined the change function won't cause a rerender of the component. It seems like you might not have fully grasped the concept of React yet, so maybe "Thinking in React" helps.

You have to store the selected value as state and update the state when the value changes. Updating the state will trigger a rerender of the component.

var MySelect = React.createClass({ getInitialState: function() { return { value: 'select' } }, change: function(event){ this.setState({value: event.target.value}); }, render: function(){ return( <div> <select onChange={this.change} value={this.state.value}> <option value="select">Select</option> <option value="Java">Java</option> <option value="C++">C++</option> </select> <p></p> <p>{this.state.value}</p> </div> ); } }); React.render(<MySelect />, document.body); 

Also note that <p> elements don't have a value attribute. React/JSX simply replicates the well-known HTML syntax, it doesn't introduce custom attributes (with the exception of key and ref). If you want the selected value to be the content of the <p> element then simply put inside of it, like you would do with any static content.

Learn more about event handling, state and form controls:

5

React Hooks (16.8+):

const Dropdown = ({ options }) => { const [selectedOption, setSelectedOption] = useState(options[0].value); return ( <select value={selectedOption} onChange={e => setSelectedOption(e.target.value)}> {options.map(o => ( <option key={o.value} value={o.value}>{o.label}</option> ))} </select> ); }; 
0
import React, { PureComponent, Fragment } from 'react'; import ReactDOM from 'react-dom'; class Select extends PureComponent { state = { options: [ { name: 'Select…', value: null, }, { name: 'A', value: 'a', }, { name: 'B', value: 'b', }, { name: 'C', value: 'c', }, ], value: '?', }; handleChange = (event) => { this.setState({ value: event.target.value }); }; render() { const { options, value } = this.state; return ( <Fragment> <select onChange={this.handleChange} value={value}> {options.map(item => ( <option key={item.value} value={item.value}> {item.name} </option> ))} </select> <h1>Favorite letter: {value}</h1> </Fragment> ); } } ReactDOM.render(<Select />, window.document.body); 
1
 handleChange(value, selectOptionSetter) => { selectOptionSetter(value) // handle other stuff like persisting to store etc } const Dropdown = (props) => { const { options } = props; const [selectedOption, setSelectedOption] = useState(options[0].value); return ( <select value={selectedOption} onChange={e => handleChange(e.target.value, setSelectedOption)}> {options.map(o => ( <option key={o.value} value={o.value}>{o.label}</option> ))} </select> ); }; 

If you are using select as inline to other component, then you can also use like given below.

<select onChange={(val) => this.handlePeriodChange(val.target.value)} className="btn btn-sm btn-outline-secondary dropdown-toggle"> <option value="TODAY">Today</option> <option value="THIS_WEEK" >This Week</option> <option value="THIS_MONTH">This Month</option> <option value="THIS_YEAR">This Year</option> <option selected value="LAST_AVAILABLE_DAY">Last Availabe NAV Day</option> </select> 

And on the component where select is used, define the function to handle onChange like below:

handlePeriodChange(selVal) { this.props.handlePeriodChange(selVal); } 

I'll add this here, in case it helps someone because this was the solution that helped me.

This is to get the SELECTED INDEX. Not for the value. (Worked for me because my options list was a list of numbers)

const [selectedOption, setSelectedOption] = useState(0) <select onChange={event => setSelectedOption(event.target.options.selectedIndex)}> 

Thank you Felix Kling, but his answer need a little change:

var MySelect = React.createClass({ getInitialState: function() { return { value: 'select' } }, change: function(event){ this.setState({value: event.target.value}); }, render: function(){ return( <div> <select onChange={this.change.bind(this)} value={this.state.value}> <option value="select">Select</option> <option value="Java">Java</option> <option value="C++">C++</option> </select> <p></p> <p>{this.state.value}</p> </div> ); } }); React.render(<MySelect />, document.body); 
1
var MySelect = React.createClass({ getInitialState: function() { 
var MySelect = React.createClass({ getInitialState: function() { return { value: 'select' } }, change: function(event){ event.persist(); //THE MAIN LINE THAT WILL SET THE VALUE this.setState({value: event.target.value}); }, render: function(){ return( <div> <select onChange={this.change.bind(this)} value={this.state.value}> <option value="select">Select</option> <option value="Java">Java</option> <option value="C++">C++</option> </select> <p></p> <p>{this.state.value}</p> </div> ); } }); React.render(<MySelect />, document.body); 
<script src=""></script> <script src=""></script>

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like