I understand how bubbling and event.stopPropagation()
works, but I'm looking for a way to avoid click handlers on all children of a DOM node.
<div onclick="console.log('LOG ME')">
<div onclick="console.log('DO NOT LOG ME')">Click me</div>
</div>
I want 'LOG ME' to be logged, but 'DO NOT LOG ME' not to be logged.
I found a CSS way using pointer-events: none
, works well, however, all my :hover, :focus CSS styles are gone on the child div.
Any ideas?
Edit: Some context. I'm using React. The outer div is a HOC, the inner one is a visible component. I want the HOC to catch all clicks of the child component, and stop click propagation into the child. In React would do:
const HOC = ChildComponent => class extends Component {
handleClick = event => { /* Do what with event? */ }
render() {
return <div onClick={this.handleClick}>
<ChildComponent />
</div>;
}
}
Where ChildComponent
is a clickable component