fatiDev
fatiDev

Reputation: 5992

unbind click doesn't work when using live

for some html element i need to remove other clicks functions using unbind or off function

but this too functions works just with click function as following :

 $("#test").click(function(){
  alert("test1");
  });
  $("#test").unbind('click').click(function(){ // or off
   alert("test2");
  });

but with live , this doesn't work and the two alerts are fired

 $("#test").live('click',function(){
  alert("test1");
  });
  $("#test").off('click').click(function(){ //unbind
   alert("test2");
  });

Upvotes: 0

Views: 758

Answers (4)

Derek Henderson
Derek Henderson

Reputation: 9706

Don't use .live(), as it's deprecated.

Use .on() instead:

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

Besides, .off() only unbinds event handlers bound with .on(). See the documentation for more information.

Upvotes: 3

Sebastian vom Meer
Sebastian vom Meer

Reputation: 5241

The opposite of .live() is .die(): http://api.jquery.com/die/

$("#test").die('click').click(function(){ //unbind
  alert("test2");
});

BTW: .live() is deprecated since 1.7. But if you use an older version of jQuery I don't see a problem using it.

Upvotes: 4

Chamika Sandamal
Chamika Sandamal

Reputation: 24302

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers.

And remember off is to use with on and die is to use with live

Upvotes: 1

PSR
PSR

Reputation: 40338

 $(document).on('click','#test',function(){
  alert("test1");
  });

Upvotes: 3

Related Questions