Chris
Chris

Reputation: 6246

Blockui is detaching events

I am using block ui to block the page and capture some information:

On the message div, I have 2 buttons. OK and Cancel.

If I set the click events on the page load:

$('#title-picker input[name=ok]').click(ok);
$('#title-picker input[name=cancel]').click(cancel);

The events are not triggered after calling $.blockUI. However if I use the .live method instead it works as intended.

$('#title-picker input[name=ok]').live('click',ok);
$('#title-picker input[name=cancel]').live('click',cancel);

I assume the mechanism it uses removes and adds the div to the DOM, and this must be what is detaching the original event handlers. I've used earlier versions of block ui before and it hasn't done this. And I can't see anything obvious in the documentation.

So, is my reasoning correct?

And are there any downsides to using .live, i.e. is there a better workaround to what I have above?

Upvotes: 0

Views: 374

Answers (1)

Irishka
Irishka

Reputation: 1136

you can use delegate instead of live

$('#title-picker').delegate('input', 'click', function(){
  if (this.name == 'ok') {
    ok();
  } else {
    cancel();
  }
//or even:  

  // window[this.name]();

  //if needed use the scope where your functions are defined 
  //instead of the window element
});

p.s.: in documentation is mentioned that :

What has changed in version 2 of the BlockUI plugin?

    Elements are no longer removed from the DOM when unblocking

Upvotes: 0

Related Questions