I have JSX code like this:
JavaScript
x
4
1
<div id="parent" onClick={clickOnParent} style={{ width: 100, height: 100 }}>
2
<div id="child" onClick={clickOnChild} style={{ width: 20, height: 20 }} />
3
</div>
4
When I click on parent
(outside the child
) it will run clickOnParent
, and when I click the child
it will run both clickOnParent
and clickOnChild
of course.
Now I want to trigger only clickOnChild
and ignore clickOnParent
when clicking exactly on the child
. How can I do?
Advertisement
Answer
JavaScript
1
5
1
const clickOnChild = (event) => {
2
event.stopPropagation();
3
4
}
5