I have a element with a click event with a child with another click event. And i did the following solution to prevent the parent action to happen when you click in the child element
const parent = document.getElementById("parent")
parent.addEventListener("click", e =>{
alert("clicked")
})
const child = document.getElementById("child")
function prevent(e)
{
e.stopPropagation()
e.preventDefault()
}
child.addEventListener("click", prevent)
child.addEventListener("mousedown", prevent)
#parent{
width:300px;
height:300px;
background-color:yellow;
display: flex;
justify-content: center;
align-items: center;
}
#child{
width:100px;
height:100px;
background-color:red;
}
<div id="parent">
<div id="child">
</div>
</div>
However when you hold down the button in the child element and release in the parent the click happens, is there a way to prevent that?