Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>stopPropagation</code> stops the event from bubbling up the event chain.</p> <p><code>preventDefault</code> prevents the default action the browser makes on that event.</p> <p>Let's say you have</p> <pre><code>&lt;div id="foo"&gt; &lt;button id="but" /&gt; &lt;/div&gt; $("#foo").click(function() { // mouse click on div }); $("#but").click(function(ev) { // mouse click on button ev.stopPropagation(); }); </code></pre> <p><strong>Example</strong></p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>$("#but").click(function(event){ event.preventDefault(); }); $("#foo").click(function(){ alert("parent click event fired !"); });</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt; &lt;div id="foo"&gt; &lt;button id="but"&gt;button&lt;/button&gt; &lt;/div&gt;</code></pre> </div> </div> </p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>$("#but").click(function(event){ event.stopPropagation(); }); $("#foo").click(function(){ alert("parent click event fired !"); });</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt; &lt;div id="foo"&gt; &lt;button id="but"&gt;button&lt;/button&gt; &lt;/div&gt;</code></pre> </div> </div> </p> <p>With <code>stopPropagation</code> only the <strong>buttons click handler</strong> is called and the <strong>divs click handler</strong> never fires.</p> <p>Where as if you just <code>preventDefault</code> only the browsers default action is stopped but the div's click handler still fires.</p> <p>Below are some docs on the DOM event objects from <code>MDN</code> and <code>MSDN</code></p> <p><strong>MDN:</strong></p> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Event/cancelBubble" rel="noreferrer"><code>event.cancelBubble</code></a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault" rel="noreferrer"><code>event.preventDefault</code></a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Event/returnValue" rel="noreferrer"><code>event.returnValue</code></a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation" rel="noreferrer"><code>event.stopPropagation</code></a></li> </ul> <p>For IE9 and FF you can just use preventDefault &amp; stopPropagation.</p> <p>To support IE8 and lower replace <code>stopPropagation</code> with <code>cancelBubble</code> and replace <code>preventDefault</code> with <code>returnValue</code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload