0

I want to write some text in an input tag only by triggering keyboard events by programming.

I don't want to using something like

elem.value = 'some random text'

I really need to simulate keyboard to write code in this input tag. pure Javascript is my goal, but if there is a good jQuery way, I will appreciate that too!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

not sure if you're looking for the infinite monkey?

function randChar() { //generate a random character - all lower case for simplicity
  var chars = " abcdefghijklmnopqurstuvwxyz";
  return chars.substr(Math.floor(Math.random() * 27), 1);
}

(not my code)

https://jsfiddle.net/concannon/d67b6y7k/

Simon
  • 66
  • 1
  • 7
  • actually I m not looking for a way to randomize picking a char. my goal is to trigger spasebar key on my code and write an " " in text bar. but only by triggering keyup, keydown or keypress events in my js code. – Sina gholami Oct 22 '21 at 12:10
  • https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key Maybe listen to the body and then update the input. – Simon Oct 22 '21 at 14:43
  • https://stackoverflow.com/questions/1846599/how-to-find-out-what-character-key-is-pressed – Simon Oct 22 '21 at 15:00
  • Do you want separate text's by pressing space ? – Ali Besharati Oct 22 '21 at 19:36