Simulate a keystroke without jquery

I have an input field where the user can write things. I want that if the user presses the "1" key to simulate another key, press "F". So, if the user types 1, the field displays 1f. How can I do this without using jQuery?

+3
source share
1 answer
var evt = document.createEvent("KeyboardEvent"); evt.initKeyEvent ("keypress", true, true, window, 0, 0, 0, 0, 13, 13); var canceled = !body.dispatchEvent(evt); 

Documentation: https://developer.mozilla.org/en-US/docs/Web/API/event.initKeyEvent

For a Webkit-based browser, initialization may be slightly different.

 initKeyboardEvent(in DOMString typeArg, in boolean canBubbleArg, in boolean cancelableArg, in views::AbstractView viewArg, in DOMString keyIdentifierArg, in unsigned long keyLocationArg, in boolean ctrlKeyArg, in boolean shiftKeyArg, in boolean altKeyArg, in boolean metaKeyArg, in boolean altGraphKeyArg); 
+4
source

Source: https://habr.com/ru/post/1242384/


All Articles