Skip to content
Advertisement

How to simulate keypress with a button click on Javascript?

I am currently working on a calculator and wondered if there is a way to link keypresses to buttons so when I click a button, it simuliates the key being pressed and sends that info to the textbox.

When I google, all the info I get is enabling buttons by keypresses but not the other way around.

If someone could help me cause my code is already a mess with all the conversations and keypress events.

Advertisement

Answer

I believe this is answered in this Stackoverflow thread: Is it possible to simulate key press events programmatically?.

To add it to the textbox you can listen to the keydown event and add the character accordingly.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement