views:

264

answers:

3

I'm trying to setup a click event on a div, and I would like that event to fire if that div is clicked anywhere except a checkbox in the div. If the checkbox is clicked, I do not want the div's click event to fire. I setup a click event on the checkbox and returned false, which stops the div's click event, but does not allow the checkbox to become checked either. Here's what I have now:

$('.theDiv').click(function() {
    // Click event for the div, I'm slide toggling something
    $('.sub-div', $(this)).slideToggle();
});

$('.checkboxInTheDiv').click(function() {
    // Do stuff for the checkbox click, but dont fire event above
    return false; // returning false won't check/uncheck the box when clicked
})
+6  A: 

Instead of returning false, which prevents the default action, try just stopping propagation.

$('.checkboxInTheDiv').click( function(e) {
    e.stopPropagation();
    ... other stuff...
    return true;
});
tvanfosson
here's a good way to do the above: http://www.quirksmode.org/js/events_order.html
JBristow
Works great, Thanks!
Ryan
A: 
Topher Fangio
A: 

Try a simple return;

Ariel