Jitender
Jitender

Reputation: 7971

unbind click event in jquery

I want to unbind click event but it is not working with live method

<script type="text/javascript">
$(function(){
$('a').live('click',function(){
alert(0)
$(this).unbind('click')
})
})
</script>


<a href="#">click</a>

Upvotes: 0

Views: 86

Answers (3)

Christophe Roussy
Christophe Roussy

Reputation: 16999

Have a look at the die method: http://api.jquery.com/die/

If you use 1.7:

$("p").live("click", foo); // ... now foo will be called when paragraphs are clicked ...
$("p").die("click", foo); // ... foo will no longer be called.

Indeed deprecated but it depends on the version you are using.

 version deprecated: 1.7, removed: 1.9

Upvotes: 1

Engineer
Engineer

Reputation: 48793

Use .one instead:

$(document).one('click','a',function(){
   alert(0);       
});

Upvotes: 2

sasi
sasi

Reputation: 4318

$(document).on('click','#a',function(){

//Code here

});

live is depricated..use on() for live()

and use this for unbind

$("p").die("click", foo);

Upvotes: 2

Related Questions