In few words, is it correct way to call keydown and keyup events, that included in each other, for same object?
For example:
$('#input_name').keyup(function() {
// some code here
$('#input_name').keydown(function(e) {
if(e.keyCode == 40) {
// another code here};
});
});
While the code is valid, it is not a good idea to assign event handlers in this way, as you are re-assigning the
keydownevent every timekeypressoccurs. The only time you would really want to do that is when you are dynamically creating and destroying elements and you need to assign handlers to them – and even then event delegation is a better way to go.Ideally you should keep event handlers separate:
If you want to run the same function for multiple events you can do this: