56

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

10 Answers 10

102

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/

Sign up to request clarification or add additional context in comments.

3 Comments

God bless you!!
Hi @Dhiraj, It is working with event.target.value also, so why above?
@user10742206 : incase if something else is assigned to "value" then its useful.
57

You can get the option text by replacing this:

event.nativeEvent.target.text;

with this:

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

Comments

11

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

e.target.selectedOptions[0].text

1 Comment

Worked with Vue!
10

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);

Comments

4

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

Comments

3

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.

Comments

2

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

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

Comments

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)
}

1 Comment

butwhat happend if you need the value as a number and get the text inside the option tag?
1

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) )
} 

Comments

1

make your dropdown accordingly and get value on change event like

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.