JsFiddle : https://jsfiddle.net/69z2wepo/9956/
I am returning a select element in the render function in my react.js
code.
But whenever I change the select
value, the function in the onChange
is not getting triggered.
var Hello = React.createClass({
render: function() {
return <select id="data-type" onChange={changeDataType()}>
<option selected="selected" value="user" data-type="enum">User</option>
<option value="HQ center" data-type="text">HQ Center</option>
<option value="business unit" data-type="boolean">Business Unit</option>
<option value="note" data-type="date">Try on </option>
<option value="con" data-type="number">Con</option>
</select>
}
});
React.render(<Hello/>, document.getElementById('container'));
function changeDataType() {
console.log("entered");
}
This function is getting triggered only once when the select is loaded and subsequently when I change the value, its not getting triggered.
To handle the onChange event on a select element in React: Set the onChange prop on the select element. Keep the value of the selected option in a state variable. Every time the user changes the selected option, update the state variable.
An onChange event handler returns a Synthetic Event object which contains useful meta data such as the target input's id, name, and current value. We can access the target input's value inside of the handleChange by accessing e. target. value.
How to use HTML <select> tag in ReactJS ? HTML <select> tag is used to create a drop down list with multiple options. The <select> tag is used as outer element and the <option> element is nested within <select> tag for defining options in a list. Approach: To achieve this functionality, we will use “App.
onChange
takes a function.
You are passing it changeDataType()
, which is running the function changeDataType
function, and setting onChange
to it's return value, which is null.
Try passing the actual function, instead of evaluating the function.
<select id="data-type" onChange={changeDataType}>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With