JavaScript How do I check if my event.target is typeof HTML input element in React? check event target class,check event target type,check event

Please see this minimum example https://codepen.io/rockmandash/pen/Rzwowd

The code is this:

enter image description here

<div class="cool">
  <input type="checkbox" value="checkbox1">
  <input type="checkbox" value="checkbox2">
  <input type="checkbox" value="checkbox3">
</div>
document.querySelector(".cool").addEventListener("click", event => {
  // In React World
  // How do I check if event.target is typeof HTML.input Element?
  console.log(event.target.value);
});

I'm asking this question because I have thousands of input elements, in react, I will have to create thousands callback function.

So I move my function up to their parent, but then I have to validate if I am current clicking an input element.

Otherwise, I could potentially set the wrong event.target.value.

Answer:1
if (event.target instanceof HTMLInputElement)
Answer:2

Use event type

handleCheck = (event) => {
  // In React World
  console.log(event.target.type);
}

<div class="cool">
  <input onClick={this.handleCheck} type="checkbox" value="checkbox1" />
  <input onClick={this.handleCheck} type="checkbox" value="checkbox2" />
  <input onClick={this.handleCheck} type="checkbox" value="checkbox3" />
</div>

DEMO

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.0/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
<div id="root"></div>

<script type="text/babel">

class App extends React.Component {
  constructor() {
    super();
    this.state = {
      name: 'React'
    };
  }

  handleCheck = (event) => {
    console.log(event.target.type);
  }

  render() {
    return (
      <div>
      <div class="cool">
        <input onClick={this.handleCheck} type="checkbox" value="checkbox1" />
        <input onClick={this.handleCheck} type="checkbox" value="checkbox2" />
        <input onClick={this.handleCheck} type="checkbox" value="checkbox3" />
        
        <input onChange={this.handleCheck} type="text" />
      </div>
      </div>
    );
  }
}

ReactDOM.render(
    <App />,
    document.getElementById('root')
);
</script>
Answer:3

I'm working on a form to add members to a project. Only members whose profiles are recorded in the system can be added. There are a name input field and a position select box. The position select box ...

I'm working on a form to add members to a project. Only members whose profiles are recorded in the system can be added. There are a name input field and a position select box. The position select box ...

  1. jquery autocomplete response example
  2. jquery autocomplete response format
  3. jquery autocomplete response function
  4. jquery autocomplete response map data
  5. jquery autocomplete response
  6. jquery autocomplete response data
  7. jquery autocomplete response not firing
  8. jquery autocomplete response label value
  9. jquery autocomplete response is not a function
  10. jquery autocomplete response event
  11. jquery autocomplete response object
  12. jquery autocomplete response callback
  13. jquery autocomplete response map
  14. jquery autocomplete response is not defined
  15. jquery autocomplete response method
  16. autocomplete jquery response json
  17. autocomplete jquery response return
  18. autocomplete response jquery
  19. jquery ui autocomplete response
  20. jquery autocomplete ajax response

I am developing a website in HTML using bootstrap and javascript. I am trying to add a toast using the following code from the bootstrap website: <div class="toast" role="alert" aria-live="...

I am developing a website in HTML using bootstrap and javascript. I am trying to add a toast using the following code from the bootstrap website: <div class="toast" role="alert" aria-live="...

  1. bootstrap toast does not work
  2. bootstrap toast is not a function

I don't know why this code won't work. I need a simple solution. This is just a college project and it doesn't have to do more then just following: I want to make ti so that when someone enters their ...

I don't know why this code won't work. I need a simple solution. This is just a college project and it doesn't have to do more then just following: I want to make ti so that when someone enters their ...

I resolve a problem which was showing up no compatibility on Edge but in Chrome. So, I changed using Object.assign(a, {}) instead of {...a, {}}. Here's where I discussed this previous problem: I ...

I resolve a problem which was showing up no compatibility on Edge but in Chrome. So, I changed using Object.assign(a, {}) instead of {...a, {}}. Here's where I discussed this previous problem: I ...

  1. script1005 expected '('
  2. script1005 script1005 expected '(' edge