I have a number of clickable elements on a page that shouldn't be enabled until a resource gets loaded. My idea was to initialize the buttons with a .disabled class, which I would then remove when the resource was ready. In the meantime, I'd have a handler on the .disabled click event that would prevent event propagation to the other handlers.
The problem is, even though I add the .disabled handler first, I can't get it to fire before the other click handlers.
$( "body" ).on('click','.disabled', function(e) {
alert('disabled');
e.stopImmediatePropagation();
return false;
});
$('.clickable').click(function(e) {
alert('enabled');
});
// Call this when ready:
//$('.clickable').removeClass('disabled');
FIDDLE: http://jsfiddle.net/4rJxU/7/
This will pop up an 'enabled' alert first, and then a 'disabled' alert, presumably because the delegated events are captured by the parent of the .clickable elements, and are thus handled later?
How can I disable click handlers using delegated events?