Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to stop event bubbling on checkbox click
    primarykey
    data
    text
    <p>I have a checkbox that I want to perform some Ajax action on the click event, however the checkbox is also inside a container with it's own click behaviour that I don't want to run when the checkbox is clicked. This sample illustrates what I want to do:</p> <pre><code>&lt;html lang="en"&gt; &lt;head&gt; &lt;title&gt;Test&lt;/title&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $('#container').addClass('hidden'); $('#header').click(function() { if($('#container').hasClass('hidden')) { $('#container').removeClass('hidden'); } else { $('#container').addClass('hidden'); } }); $('#header input[type=checkbox]').click(function(event) { // Do something }); }); &lt;/script&gt; &lt;style type="text/css"&gt; #container.hidden #body { display:none; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="container"&gt; &lt;div id="header"&gt; &lt;h1&gt;Title&lt;/h1&gt; &lt;input type="checkbox" name="test" /&gt; &lt;/div&gt; &lt;div id="body"&gt; &lt;p&gt;Some content&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>However, I can't figure out how to stop the event bubbling without causing the default click behaviour (checkbox becoming checked/unchecked) to not run.</p> <p>Both of the following stop the event bubbling but also don't change the checkbox state:</p> <pre><code>event.preventDefault(); return false; </code></pre>
    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.
 

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