I want to get attribute from my select option list.
In meanwhile my code is like below :
var Billing = React.createClass({
getInitialState() {
return {
associations: [],
value: '',
associationsList: '1'
};
},
handleChange(event) {
this.setState({value: event.target.value});
},
handleOption(event){
var option = event.target.getAttribute('data-id');
this.setState({associationsList: option});
},
render() {
var listAssociations = this.state.associations.map(function(index){
return (
<option onChange={this.handleOption} value={index.name} data-id={index.id} ref={index.name} key={index.id}>{index.name}</option>
)
});
var BillingInfo
{
if(this.state.associationsList == "0")
{
return (
<div>
<Tabs selected={0}>
<Pane label="Billing Info">
<div className="row">
<div className="small-12">
Associations:
<select value={this.state.value} onChange={this.handleChange}>
{listAssociations}
</select>
</div>
</div>
<div>
{this.state.value}<br/>
{this.state.associationsList}
Basic package
</div>
</Pane>
<Pane label="Billing History">
<div>Billing history</div>
</Pane>
</Tabs>
</div>
);
I have define option value with "index.name" i want to retrieve "data-id" to get different output. Can someone help me in this ?
I have updated my code like below. But still i cannot get the data-id or is there any other method i can get my data id without set "value" with "id".
handleChange(event) {
var index = event.target.selectedIndex;
var optionElement = event.target.childNodes[index]
var option = optionElement.getAttribute('data-id');
this.setState({
associationsList: option,
value: event.target.value
});
},
render() {
var listAssociations = this.state.associations.map(function(index){
return (
<option value={index.name} data-id={index.id} ref={index.name} key={index.id}>{index.name}</option>
)
});
var BillingInfo
{
if(this.state.associationsList == "0")
{
return (
<div>
<Tabs selected={0}>
<Pane label="Billing Info">
<div className="row">
<div className="small-12">
Associations:
<select value={this.state.value} onChange={this.handleChange}>
{listAssociations}
</select>
</div>
</div>
<div>
{this.state.value}<br/>
{this.state.associationsList}
Basic package
</div>
</Pane>
<Pane label="Billing History">
<div>Billing history</div>
</Pane>
</Tabs>
</div>
);
}
Getting the Selected Value To fetch the selected value from the select element, you can use the onChange event handler prop. Just like the input or textarea elements, you can use the onChange event handler to get the value from the event object.
The text of an option is simply the label property of the corresponding item . In your case, to retrieve the text of the selected option, you can do: var selectedItem = this.
The getAttribute() method will return the string value of the specified attribute. Alternatively, you can also access attributes using the getNamedItem() method.
js import React from 'react'; import Select from 'react-select'; ... With those two packages imported, we will be able to have access to the react-select ( <Select />) and also extend the React. Component class. In traditional HTML, the <select> tag houses multiple options and values.
You can get the index of the option
element that is currently selected in the onChange event in select
:
handleChange(e) {
var index = e.target.selectedIndex;
var optionElement = e.target.childNodes[index]
var option = optionElement.getAttribute('data-id');
this.setState({
associationsList: option,
value: event.target.value
});
}
This will mean there will be no need to have a onChange handler in the option
elements (handleOption
).
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