Pieter Moeyersons
Pieter Moeyersons

Reputation: 17

jQuery unbind/die live event

I have a simple app to add quotes and the possibility to like the quote (not FB like). I have a simple div as a button with following children: a span with the total likes and a paragraph for some text. I need to work with .live() because some of the content is loaded in with AJAX. I need to unbind/die the event because once you clicked the div can't be clicked anymore. But because with .live() you can't use $(this) I tried working with event.target but this has some problems.

My HTML:

<div class="btnlike">
<span>0</span>
<p>Click to vote</p>
</div>

My Javascript (so far with the event.target)

$(".btnlike").live('click',function(event){     

    var $target;
    if( $(event.target).is("div") ) {
        $target = $(event.target);
    } else {
        $target = $(event.target).parent();
    }

    $target.unbind(event);      

});

But I would like to use .die() but because $(this) doesn't work are there any other solutions?

Upvotes: 0

Views: 846

Answers (1)

jeremyharris
jeremyharris

Reputation: 7882

One solution would be to just check if it's been executed on that element within the live function.

$(".btnlike").live('click',function(event){     
    if ($(this).data('clicked')) {
      // already ran it
      return; 
    }
    // mark it as being run
    $(this).data('clicked', true);
});

Upvotes: 1

Related Questions