stopPropagation works but cannot stop href being executed on child click, is there a way to make parent anchor do not execute it's href on child element is clicked(stopPropagation/bubbling)?
Here is my markup:
<div id="parent">
<h5>Parent</h5>
<a id="childAnchor" href="https://www.google.com/" target="_blank">
<h5>Child anchor tag</h5>
<div id="grandChildAnchor">
<h5>Grand Child div tag</h5>
</div>
</a>
</div>
Here is jsfiddle where I am redirecting on anchor click: https://jsfiddle.net/gchc9Ldb/
Here is stackoverflow code snippet but the redirect (target="_blank") is not happening correctly. So check the above jsfiddle link rather below code snippet.
$('#parent').click(function() {
console.log('#parent clicked');
})
$('#parent a').click(function() {
console.log('#parent > anchor clicked');
})
$('#parent a div').click(function(e) {
e.stopPropagation();
console.log('#parent > anchor > clicked');
})
h5 {
text-align: center;
color: white;
margin: 0;
}
#parent {
width: 200px;
height: 200px;
padding: 10px;
background-color: green
}
a {
display: block;
padding: 50px 10px;
background-color: blue
}
#grandChildAnchor {
padding: 25px 0;
background-color: red
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="parent">
<h5>Parent</h5>
<a id="childAnchor" href="https://www.google.com/" target="_blank">
<h5>Child anchor tag</h5>
<div id="grandChildAnchor">
<h5>Grand Child div tag</h5>
</div>
</a>
</div>
EDIT:
I don't want to run return false
on my anchor tag's href, I still need href to work but not when the anchor tag's child is clicked. e.stopPropagation
will not work for me. Hence, it's not a duplicate question as most assumed.