Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Two options (both involve removing the <code>tr:not</code> stuff from your existing code, which as you say doesn't work&nbsp;&mdash; <code>tr</code> elements can't be checkboxes, and <code>:not</code> checks the element, not its contents):</p> <ol> <li><p>Add an event handler to the checkbox that calls <a href="http://api.jquery.com/event.stopPropagation/" rel="nofollow"><code>e.stopPropagation</code></a>. Then the click event won't reach the row. You can do that either directly or via delegation. <a href="http://jsbin.com/ewoder" rel="nofollow">Here's a live example</a> going direct. If you go indirect, be sure to test clicking <code>label</code>s that activate the checkbox (if you're going to have them) on all of the browsers you intend to support.</p> <p>or</p></li> <li><p>Add this to your handler:</p> <pre><code>if ($(event.target).is('input[type=checkbox]')) { return; } </code></pre> <p>E.g.:</p> <pre><code>$('table').on('click', 'td', function(event) { if ($(event.target).is('input[type=checkbox]')) { return; } // Logic here }); </code></pre> <p>That works by testing the source of the event to see if it's a checkbox, and bailing early.</p></li> </ol> <p>In both cases, if you use a <code>label</code> to activate the checkbox, you may need to do the same thing for the label.</p> <p>I got curious about what #2 would look like handling <code>label</code>s, and it turns out it's enough code to move into a function, but not hard&nbsp;&amp;mdash probably how I'd go: <a href="http://jsbin.com/ewoder/4" rel="nofollow">Live example</a> | <a href="http://jsbin.com/ewoder/4/edit" rel="nofollow">source</a></p> <pre><code>jQuery(function($) { // The table cell click handler $("table").on("click", "td", function(e) { // Is the source a checkbox or the label for // one? if (isCheckbox($(e.target))) { return; } // Normal handling $(this).toggleClass("foo"); }); // Function to test whether the source is a // checkbox, or the label of a checkbox function isCheckbox($elm) { var chkid; if ($elm.is("input[type=checkbox]")) { return true; } if ($elm.is("label")) { chkid = $elm.attr("for"); if (chkid) { return $("#" + chkid).is("input[type=checkbox]"); } return !!$elm.find("input[type=checkbox]")[0]; } return false; } }); </code></pre>
    singulars
    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