Reputation: 4592
I'm trying to get the keydown
event to fire whenever the Delete key is pressed while in a certain context of my web application. For example, I have an online file system implemented, and when that view is in use, I want the keydown
event to be active.
However, I also have certain popups that come up over the page when certain actions are performed. When these popups are visible, I'd like the keydown
event to be disabled, because that event interferes with their functionality. The working event code that I have currently doesn't do this:
$(document).keydown(function(event){
if(event.which === 46 && ($('.selected').size() > 0)){
saveDelete();
}
});
Is there a way to have this keydown
event stop firing under certain conditions?
Upvotes: 0
Views: 633
Reputation: 287
I am not 100% sure but try this-
window.onkeydown(function(){
//code goes here
});
Upvotes: 0
Reputation: 20323
Change your code to use .length
instead of size.
When you popup is visible you can unbind the event handler by calling .off
and when your popup is not visible or user closes it you can again reattach the keyevent by calling .on
, this will work if you are using jQuery 1.7 for earlier releases you will have to use .bind
and .unbind
Upvotes: 0