I encountered this situation recently (simplified here). Simply wrap a checkbox with an element and apply preventDefault() on it's click event and the checkbox becomes uncheckable.
See this fiddle, but here's a snip:
<div>
<input type="checkbox"/>
</div>
/* Wrapper could be any element (and any ancestor element will work) */
$('div').on('click', function(e){
e.preventDefault();
});
/* Uncomment to make the checkbox work again
$('input').on('click', function(e){
e.stopPropagation();
});
*/
The behavior occurs in Chrome and FF, so I assume it is intentional.
Why does the click event, which has already been triggered on the checkbox itself not result in the checkbox getting toggled? The preventDefault
on the ancestor seems like it ought to be irrelevant to the child checkbox's behavior. It seems as if, for the checkbox change to occur, the click event needs to bubble freely all the way to the document root.
What's going on here?
preventDefault()
. It doesn't matter what node the eventObject is bubbling through, it's still the same object. – Via