Here is a jsfiddle that demonstrates your code scenario:
http://jsfiddle.net/sAfTT/
The problem you're running into is a common one with events in HTML. When events are fired, they are fired on every element to which they can be applied in order from closest to furthest away. This is called bubbling(theres also capturing which works in reverse). http://www.quirksmode.org/js/events_order.html
So, in reality, when you are clicking on the checkbox, you are also clicking on the row, so the handler call looks like this.(assuming the box is unchecked to start with. reverse check/uncheck as applicable)
- Checkbox clicked: check the box
- Can I bubble up? Yes
- Row clicked. Is there an event handler? Yes
- Fire event handler (yours). This determines if the checkbox is checked. It is.
- Because the box is checked, uncheck it.
- Can I bubble up? Yes
- Table Clicked. Is there an event handler? no
- Document Clicked. Is there an event handler? No.
you can attach an event handler to the checkbox directly to prevent bubbling
$(':checkbox').click(function(e){
e.preventDefault();
e.stopPropagation();
})
edit: I could never get the prevent default to work just right in jsfiddle, but the concept provided by the other answers work nicely too.