Wednesday, June 22, 2016

What's the difference between event.stopPropagation and event.preventDefault?

stopPropagation stops the event from bubbling up the event chain.
preventDefault prevents the default action the browser makes on that event.
Let's say you have
<div id="foo">
 <button id="but" />
</div>

$("#foo").click(function() {
   // mouse click on div
});

$("#but").click(function(ev) {
   // mouse click on button
   ev.stopPropagation();
});