Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Unbind event when removing a class

Tags:

jquery

So I have a couple of divs with a class "turn". Then in Jquery I have

$(".turn").click(function(){
   ...some code...
   if (..) $(this).removeClass("turn");
});

which I expect to remove the handler, so I can't click on it anymore. But I still want to be able on the other divs that has that class. This is not working (I believe is because JQuery doesn't remove the handlers when a class is removed) I've also tried with

$('.turn?).live(click,...) and $(this).die() 

and

$('.turn?).bind()/$(this).unbind()

But neither of those seems to work. Can I get some hint about how to do this? Thanks

like image 987
dgiulian Avatar asked May 14 '26 11:05

dgiulian


1 Answers

The correct method for this is to use unbind

$(".turn").click(function(){
   ...some code...
   if (..) $(this).unbind("click");
});
like image 184
Joseph Marikle Avatar answered May 17 '26 01:05

Joseph Marikle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!