question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get name of select component onChange

See original GitHub issue

Is it possible to detect what select component triggered an onChange event? Doesn’t look like it in the docs, but maybe there is a workaround?

Use case: I have several select components that are filtering a table, right now every select component needs its own handleChange function triggered by OnChange, but all I need is the name to use as a key to make my updates.

e.g. would be nice to do something like:

constructor() {
  this.state = {
    branchFilter: '',
    moduleFilter: ''
  }
}

handleFilterChange(newValue, [selectedOptions], name) {
   this.setState({ 
       [name]: newVal
   });
}

render() {
   return (
      <div className='branch-filter'>
        <Select
          name="branchFilter"
          value={this.state.branchFilter}
          options={uniqueBranches(this.props.branches)}
          onChange={this.handleBranchFilterChange}
        />
        <Select
          name="moduleFilter"
          value={this.state.moduleFilter}
          options={uniqueModules(this.props.branches)}
          onChange={this.handleModuleFilterChange}
        />
      </div>

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:23
  • Comments:21 (1 by maintainers)

github_iconTop GitHub Comments

23reactions
MadeInMooncommented, Feb 26, 2017

The following code works for me

class MyComponent extends React.Component {
  
  onSelectChange(name, value) {
    let obj = {};
    obj[name] = value;
    this.setState(obj);
  }
  
  render() {
    return (
        <Select name="myName" onChange={this.onSelectChange.bind(this, 'myName')}/>
    );
  }
  
}

and this one for a .map() iterator

const Selects = [
  {
    name: 'firstSelect',
    options: [
      {value: 'one', label: '1'},
      {value: 'two', label: '2'},
    ],
  },
  {
    name: 'secondSelect',
    options: [
      {value: 'one', label: '1'},
      {value: 'two', label: '2'},
      {value: 'three', label: '3'},
    ],
  },
];
    
class MyC extends React.Component {

  onSelectChange(name, value) {
    let obj = {};
    obj[name] = value;
    this.setState(obj);
  }

  render() {
    return (
      <div>
        {Selects.map((select, i)=>{
          return (
            <Select
              key={i}
              name={select.name}
              options={select.options}
              onChange={this.onSelectChange.bind(this, select.name)}
            />
          );
        })}
      </div>
  }

}
17reactions
atomoccommented, Oct 25, 2017

The first thing that a user will encounter most often is stuck on this issue!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get the name of the select with onchange event - Stack Overflow
It gives the name of select in console. function myfunction(Element) { console.log($(Element).
Read more >
onchange Event - W3Schools
The onchange event occurs when the value of an element has been changed. ... The other difference is that the onchange event also...
Read more >
Handle the onChange event on a Select element in React
To handle the onChange event on a select element in React, set the `onChange` prop on the select element. Keep the value of...
Read more >
API - React Select
Get the styles of a particular part of the select. Pass in the name of the property as the first argument, and the...
Read more >
HTMLElement: change event - Web APIs | MDN
The change event is fired for <input> , <select> , and <textarea> elements when the user modifies the element's value.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found