Reputation: 77
jQuery:
$(document).keyup(function(e)
{
if(e.keyCode == 27)
{
hide_popup();
}
else if(e.keyCode==13)
{
do_submission();
}
});
At some situation, I need to unbind certain keypress codes. Say when a popup is closed, I need to unbind the keycode 27. Or if a form is submitted, I need to unbind only the keycode 13. Is there anyway to unbind certain keycodes only, since $(document).unbind('keyup')
removes all these keycodes?
Upvotes: 1
Views: 152
Reputation: 337580
Don't unbind the entire event. Instead have a flag variable which is set by other functions to indicate whether the enter keypress should be handled. Something like this:
var processEnterKeyPress = true;
$(document).keyup(function(e) {
if (e.keyCode == 27) {
hide_popup();
}
else if (processEnterKeyPress && e.keyCode == 13) {
do_submission();
}
});
$('#myForm').submit(function() {
processEnterKeyPress = false;
});
Upvotes: 2