0

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?

1
  • If you are calling a function after the resource is loaded, why not just attach your click handlers then? Commented Oct 17, 2013 at 17:21

2 Answers 2

1

This should work (without the need for the disabled class):

var clickEnabled = false;

$('.clickable').on('click', function(e) {
    if (!clickEnabled) {
        alert('disabled');
        e.stopImmediatePropagation();
        return false;
    }
    else {
        alert('enabled');
    }
});

... And just set clickEnabled to true once whatever you need is ready.

Fiddle: http://jsfiddle.net/ehFGX/

If all you want is for the default click handler to fire once everything is ready, you can just remove the else block above and it will continue with the default if your handler doesn't return false.

If you still prefer to use the disabled class, you can just check if $(this) has that class instead of checking !clickEnabled, too.

Sign up to request clarification or add additional context in comments.

1 Comment

Bingo- I ended up keeping the .disabled class, but checking for it in the revised handler just like you describe. jsfiddle.net/4rJxU/9
1

Isn't it better to add variable which will hold actual status?

var isLoaded = false;

$( "body" ).on('click', function(e) {
    if (isLoaded) {
        alert('enabled');   
    } else {
        alert('disabled');
        e.stopImmediatePropagation();
        return false;
    }
});

And then change it to true when everything is ready.

2 Comments

That doesn't solve my problem. I could just as easily check for the .disabled class in each handler as well. I wanted a solution that wouldn't require a check in each handler, but that would let me turn on/off all the handlers en masse.
this switch could work well if applied to $(".clickable") instead of $("body")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.