Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

e.target.id not working react js

//Well if the elements are nested event.target won't 
//always work since it refers to the target that triggers the 
//event in the first place. See this link for the usage of event.currentTarget, 
//which always refer to the element that the handler is bound to.

class Button extends React.Component {

    getButtonId = (e) => {
      console.log(e.currentTarget.id);
    }

    render() {
       return (
          <button id="yourID" onClick={this.getButtonId}>Button</button>
       );
    }
}
 
PREVIOUS NEXT
Tagged: #working #react #js
ADD COMMENT
Topic
Name
8+4 =