// In vanilla JavaScript, each event type requires its own event listener.
// Unfortunately, you can’t pass in multiple events to a single listener like you might in jQuery and other frameworks.
var element = document.getElementById(‘element_id’);
element.addEventListener(‘keypress’, dosomething, false);
element.addEventListener(‘keydown’, dosomething, false);
// es6
[‘keydown’,‘keypress’].forEach( evt =>
element.addEventListener(evt, dosomething, false)
);
// es5
[‘keydown’,‘keypress’].forEach( function(evt) {
element.addEventListener(evt, dosomething, false);
});
#javascript #es6 #es5