I'm using React and I have the following situation.
I have one parent div with onClick event, which takes full width and inside that div I have an image. I want to be able to know where it is clicked. Thus, I want to know if image (child) or div(parent) is clicked.
My code is as follows:
class APP extends React.Component { constructor(props) { super(props) } render() { return ( <div className="parent" onClick={(e) => console.log("PARENT CLICK")}> <img src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg" style={{maxWidth: "60%", maxHeight: "90%", pointerEvents: 'none', zIndex: 99999}} onClick={e => console.log("IMAGE CLICK")} /> </div> ) } } ReactDOM.render(<APP />, document.querySelector("#app")) But it detects always the parent click. I tried to add z-index to the child, but I think that child can't be in front of parent.
e.stopPropagationfor your use-case. See my updated answer.onclickfunctions in his code, that's whye.stopPropagationis required.