E.g
<div class="container">
<div class="inside">I am not fire when click me</div>
</div>
$('.container').click(function(){
// container do something here
});
but,when I click the div inside it also trigger the container's click event because the div is inside the container, so , I need a way to prevent the container event trigger when I click on the inside div!
Thank you very much!!
You can cancel actions caused by row click events by calling event. stopImmediatePropagation() in an onRowClick event handler. If necessary, you can specify the same code for onCellClick.
By a adding an onClick function for the child modal (content div) mouse click events are prevented to reach the 'closeLogin' function of the parent element.
The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. For example, this can be useful when: Clicking on a "Submit" button, prevent it from submitting a form. Clicking on a link, prevent the link from following the URL.
As a more general solution, you should check e.target
in container
's click
event handler.
$('.container').click(function(e) {
// If you want to ignore clicks to anything inside the `.container` div:
if (!$(e.target).hasClass('container')) return;
// or, if you only want the `.inside` div to not fire the event,
if ($(e.target).hasClass('inside')) return;
// container do something here
});
This way you're not preventing propagation of the event, which would break bound live
handlers.
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