Rupesh Kahane

Rupesh Kahane

  • 101
  • 19.1k
  • 4.2m

programmatically press keyboard key on button click event

Mar 24 2017 6:48 AM
I have one textbox & button. On button click event I have too show some keyboard value (example I have to show number 2 into a textbox)
 
How can I do a javascript code which will automatically press a key & show a text into a textbox.
 
I tried with below code. Suggest me something.
Thanks in advance. 
  1. <input type="text" id="myInputTextBoxId" value="">    
  2. <button id="myButton" type="button">Generate Text</button>    
  3.     
  4. <script>    
  5. document.getElementById("myButton").addEventListener("click"function() {    
  6.         var press = jQuery.Event("keypress");    
  7.         press.which = 50;    
  8.         $('#myInputTextBoxId').trigger(press);    
  9.         $('#myInputTextBoxId').focus();    
  10. });    
  11. </script>    
 

Answers (3)