tags:

views:

45

answers:

3

Hi I've just started experimenting with .live and .die and having some great results but one thing isn't working.

I've been tinkering with firebugs console to try out my written code live to see if i can figure out the reason why .die isn't killing off an attached event.

First if i do this

//attach ajax submission
    $('a[href$=edit]').live("click", function(event) {
        $.get($(this).attr("href"), null, null);
        return false;
    });

Then as expected when I click on a link the ajax fires off and my server side code injects a form for inline editing.

But sometimes I want to disable this behaviour and also make the link unclickable so I do the following

//unbind ajax form creation when we click on a link, then disable its semantic behaviour
    $('a[href$=edit]').die("click").click( function(){  return false; } );

which works but if then try to remove this and restore that ajax goodness with the code below it doesn't work, Instead the link remains unclickable. I cant figure out why? Can anyone help?

//remove any previous events from the links
    $('a[href$=edit]').die();
    //attach ajax submission
    $('a[href$=edit]').live("click", function(event) {
        $.get($(this).attr("href"), null, null);
        return false;
    });
+1  A: 

You have to do this:

$('a[href$=edit]').die().unbind('click');

This part of your code .click( function(){ return false; } ); isn't .live(), it's a normal .bind('click') declaration, so you need to kill it with .unbind('click').

Nick Craver
thank you for pinpointing where i was going wrong. I missed that stray click though ive looked at the code for hours. Thank you very much.
adam
@adam - Welcome :)
Nick Craver
+1  A: 

die only works for events bound by live.

You're return false event is bound using click, which is an alternate face of bind. Events bound in this way need to be unbound using unbind.

Matt
thanks for the head ups. all good now.
adam
+1  A: 

click and live(click) do not have the same inner-workings :

  1. click or bind(click) immediatly adds an event to each of the elements of the jQuery selector
  2. live(click) waits for a click somewhere an then checks if that click has been done on an element that matches the jQuery selector.

Calling die is the counterpart of live : you stop the pattern #2, not the pattern #1

If you want to remove an event that has been added via the pattern #1, you will need to call unbind

Jerome Wagner

Jerome WAGNER
ahhhh i missed missed that direct call to click. Thank you very much. Got it working now.
adam