I have a parent <div />
that covers the whole page. Within this parent <div />
I am having a smaller child <div />
. I do not want the child <div />
to be clickable, when the parent <div />
overlays the child `.
function parentFunction(event) {
if (document.getElementById("check").checked) {
event.stopPropagation();
}
}
function childFunction() {
alert("CHILD");
}
.child {
padding: 50px;
width: 100px;
height: 100px;
background-color: hotpink;
text-align: center;
cursor: pointer;
}
.parent {
width: 500px;
height: 500px;
filter: blur(2px);
display: flex;
justifc-content: center;
align-items: center;
align-content: center;
background-color: lightgray;
}
<body>
<div
onclick="parentFunction(event)"
class="parent">
<div
onclick="childFunction()"
class="child">
Child
</div>
</div>
</body>
Stop propagation:
<input type="checkbox" id="check">
3
Answers
As it was previously stated the propagation goes from child to parent and not the other way around.
Possibilities you have:
or
Here’s a solution that would likely answer your need.
When you click on the Parent, which holds a child inside, it will tell you which element was clicked. Then you can if yourself into the destination/solution you require in the functionality.
It is on the other way around, when you click on the
child
you don’t want the event to be propagated to the parent which is overlapping it, so it should be instead: