Bomber
Bomber

Reputation: 10957

Get selected option text using react js?

I have my select list component rendering my select list:

<form className="pure-form">
<select ref="selectMark" className="mark-selector"
 onChange={this.onChange}>{this.getOptions()}
</select>
</form>

I have a method on the component to create the options:

getOptions: function () {
    return this.props.renderProps.data.map(function (item) {
        return <option key={item.value} value={item.value}>{item.label}</option>;
    }.bind(this));

},

My onChange method works fine with the value:

onChange: function(event) {
    var newValue = event.nativeEvent.target.value;
    this.props.renderProps.onSaveCare(newValue);
    this.setState({value: newValue});
    this.toggleEdit();
},

Is there a way I can get the option text? This gives me undefined

event.nativeEvent.target.text; //undefined

Upvotes: 56

Views: 88766

Answers (10)

Vinicius Ribeiro
Vinicius Ribeiro

Reputation: 124

It's easy using refs.

import the hook

import React, { useContext, useState, useEffect, useRef } from "react";

instantiate it

const yourNewRef= useRef();

Reference it in your element

ref={yourNewRef}

Then you can access it in any event or function by simply calling:

let textSelectOption = yourNewRef.current.options[yourNewRef.current.selectedIndex].text

You can access the value simply by calling

let optionValue = yourNewRef.current.value

refs are great, you can do almost everything you would using jQuery. Take a look at yourNewRef on console and access all the content via the .current property

Upvotes: 3

muhammad osama
muhammad osama

Reputation: 21

make your dropdown accordingly and get value on change event like

onChange = {(e,index)  =>
( setAddressChangeType(e.target.value),  console.log(index.props.id) )
}  

Upvotes: 1

muhammad osama
muhammad osama

Reputation: 21

change your menu item accordingly

Current / Temporary address Permanent address Office / Business Address

and on change event get

onChange = {(e,index)  =>
( setAddressChangeType(e.target.value),  console.log(index.props.id) )
} 

Upvotes: 1

2020 and this Worked For Me

My Select Element :

          <FormGroup>
                  {<Select
                    closeMenuOnSelect={true}
                    components={animatedComponents}
                    options={Options}
                    value = "Ahmedabad"
                    onChange={this.handleChange}
                    name = "citySelect"
                  />}
          </FormGroup>

Call handler :

handleChange = (e) => {
    console.log(e.value)
}

Upvotes: 1

Dinesh Katwal
Dinesh Katwal

Reputation: 950

Here what i do to retrieve the text from select option in react js.

this.refs.selectMark[this.refs.selectMark.value].text

Upvotes: 2

Olbor
Olbor

Reputation: 91

This worked for me

const {options, value} = e.target;
console.log(options[value].innerHTML);

Edit: I just realized I was using the "value" field to store the ID of some objects, from 0 to n. I guess a better approach could be the following:

const {options, selectedIndex} = e.target;
console.log(options[selectedIndex].innerHTML);

Upvotes: 9

Sean M
Sean M

Reputation: 2030

You can get the option text by replacing this:

event.nativeEvent.target.text;

with this:

event.target.options[event.target.selectedIndex].text

Upvotes: 57

user2091375
user2091375

Reputation: 119

If it's single select, here is more simple way:

e.target.selectedOptions[0].text

Upvotes: 11

Alexandre Kirszenberg
Alexandre Kirszenberg

Reputation: 36418

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.props.renderProps.data.find(function (item) {
  return item.value === event.target.value;
});
selectedItem.label;

Array.prototype.find is part of the ES6 proposal. Underscore or lodash already package it as the _.find method.

Upvotes: 3

Dhiraj
Dhiraj

Reputation: 33618

Something like this should do

var index = event.nativeEvent.target.selectedIndex;
event.nativeEvent.target[index].text

Here is a demo http://jsbin.com/vumune/4/

Upvotes: 102

Related Questions