Yes, this can be done using initKeyEvent . However, this is a little useful. If this bothers you, use jQuery as shown in @WojtekT .
Otherwise, in vanilla javascript, here is how it works:
// Create the event var evt = document.createEvent( 'KeyboardEvent' ); // Init the options evt.initKeyEvent( "keypress", // the kind of event true, // boolean "can it bubble?" true, // boolean "can it be cancelled?" null, // specifies the view context (usually window or null) false, // boolean "Ctrl key?" false, // boolean "Alt key?" false, // Boolean "Shift key?" false, // Boolean "Meta key?" 9, // the keyCode 0); // the charCode // Dispatch the event on the element el.dispatchEvent( evt );
source share