After click on a button my script loads new content. As long as it is loading I don't want a list of elements to be clickable.
IS there any way to unbind the click-event of those elements, load the content and then "rebind" the events correctly?
What I want to prevent is a if-statement inside every click-function/element.
Here is what i've tried:
$(load_button).click(function(){
$('a').each(function(){
var dis_e = $(this).data('events');
$(this).data('disabled-events', dis_e);
}).unbind('click');
$(some_content).load(function(){
$('a').each(function(){
var dis_e = $(this).data('disabled-events');
$(this).removeData('disabled-events');
$(this).bind('click', dis_e);
});
});
});
UPDATE:
it should also prevent the elements from (in this case) alerting 'click':
$('a').click(function(){
alert('click');
});
You can temporarily bind a new click event with a specific namespace for your elements that will prevent the click event to propagate and when your content is loaded you unbind your event.
And later on:
EDIT: As pointed out by Yoshi, this expects your click event handler to be attached to the element before your custom events. So I can see two ways around this:
stopImmediatePropagation()
andreturn false
when you are loading your data.If you chose the first method, your event would be something like this :
and then when loading your content you set the variable
clicDisabled
to true temporarily.Based on your answers i wrote the following plugin:
Instead of using a global variable i decided to add/remove the class 'disabled' in order to also have control within css.
check out the demo!
THX to all!
UPDATE: Check this out !!