I want .click to do nothing IF the user clicks <p>text</p>
inside the div, for everything else I want the div .click to work.
HTML:
<div>
<span>yes</span>
<p>text</p>
</div>
Jquery:
<script>
$(document).ready(function() {
$("div").click(function() {
alert("woohoo!");
});
});
</script>
$("div").click(function(e) {
if($(e.target).is('p')){
e.preventDefault();
return;
}
alert("woohoo!");
});
check the target of the click. this way you dont need to bind another event.
<script>
$( document ).ready(function() {
$( "div :not(p)" ).click(function( event ) {
alert( "woohoo!" );
});
});
</script>
This implies you don't care about text nodes as children of the <div>
container.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With