3

I am working on a ReactJS application and I am trying to get the text of a selected option in a dropdown (Semantic UI component);

exposedCampaignOnChange = (e, {value}) => {
    this.props.campaignExposedSelected(value);
};

<Dropdown
    placeholder='Campaign Exposed To'
    fluid
    search
    selection
    multiple
    options={this.state.campaigns}
    onChange={this.exposedCampaignOnChange}
/>

The above code returns the value. this.state.campaigns is made up of an array of objects with value and text properties. In addition to the value, I also want to get the textvalue of the selected options.

Appreciate any guidance on the matter.

3 Answers 3

5

You can use synthetic event's target property to get the text like:

exposedCampaignOnChange = (e, {value}) => {
  e.persist();
  console.log(e.target.textContent);
  this.props.campaignExposedSelected(value);
};

<Dropdown
  placeholder='Campaign Exposed To'
  fluid
  search
  selection
  multiple
  options={this.state.campaigns}
  onChange={this.exposedCampaignOnChange}
/>
Sign up to request clarification or add additional context in comments.

2 Comments

Better not target, but currentTarget: github.com/DefinitelyTyped/DefinitelyTyped/issues/…
e.taget.innerHTML worked for me too as the output of the dropdown option was a div > span > text
1

Semantic ui react Dropdown onChange event takes two arguments - onChange(event: SyntheticEvent, data: object). You don't need to pass them explicitly while calling the function.

exposedCampaignOnChange = (e, value) => {
  e.persist();
  this.props.campaignExposedSelected(value);
};

Comments

0

data/value should have selected option text like in semantic UI

function getSelectedTextValue() {
  alert( $('.ui.dropdown').dropdown('get text') + " : " + $('.ui.dropdown').dropdown('get value') );
}

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.