Search code examples
jqueryevent-handlinglistenerjquery-events

Using .off() while it is nested inside of .on()


Is it possible to unbind a listener from within the listener? I want to do this because I have a listener which determines the criteria of its own unbinding (e.g. when a certain key is pressed, the listener should unbind).

$input.on("keydown.listenerOne", function (press) {  
   if(press=="37"){
      $input.off("keyup.listenerOne");
   }
});

Solution

  • This won't work because you're binding on the keydown event and unbinding the keyup event, which has no binding per your example. Also the event you reference as press would be e or event and you'll need to reference the which property: if(e.which == 37)