1

I use a jquery function on a div.class. In the function I change the class on $(this) so that the function may only be called once per div. Inspecting the element with Firefox web developer options shows the class as changed, yet the click event for the original class will still fire on the element with the new class. Code below; I also tried to implement this with toggleClass, exact same results. Any suggestions?

$('div.cluebox').click(function(e) {        
    $( this ).removeClass("cluebox").addClass("emptyBox");;     
});

Thanks

Dutchie432
  • 28,798
  • 20
  • 92
  • 109
  • 2
    Event handlers aren't bound to classes, they are bound to elements. Even if you change the class, the element still has the same handler bound to it. You can get around this using event delegation with `on()`. – Jason P Sep 27 '13 at 14:49
  • Thanks, working now with one() – user2823852 Sep 27 '13 at 15:05

3 Answers3

1

When you bind an event (click) to an object, the event stays bound to it until you unbind it. You may be able to simply use the on function instead of click()

Dutchie432
  • 28,798
  • 20
  • 92
  • 109
1

If you want bind event once use jQuery one()

http://api.jquery.com/one/

Maciej Pyszyński
  • 9,266
  • 3
  • 25
  • 28
1

Try with:

$('div.cluebox').click(function(e) {        
    $( this ).removeClass("cluebox").addClass("emptyBox");
    $( this).off("click");
});
Vishkey
  • 197
  • 6