I have a link on a webpage. When a user clicks it, a widget on the page should update. However, I am doing something, because the default functionality (navigating to a different page) occurs before the event fires.
This is what the link looks like:
<a href="store/cart/" class="update-cart">Update Cart</a>
This is what the jQuery looks like:
$('.update-cart').click(function(e) {
e.stopPropagation();
updateCartWidget();
});
What is the problem?
You can use
e.preventDefault();
instead ofe.stopPropagation();
from https://developer.mozilla.org/en-US/docs/Web/API/event.preventDefault
You want
e.preventDefault()
to prevent the default functionality from occurring.Or have
return false
from your method.preventDefault
prevents the default functionality andstopPropagation
prevents the event from bubbling up to container elements.The following methods achieve the exact same thing.
This code strip all event listeners