0

I'm working on a project, and i have to trigger React onKeyPress event with Javascript, I was able to trigger the event in Mozilla with the following code:

var evt = document.createEvent("KeyboardEvent"); 
evt.initKeyEvent("keypress", true, true, window, false, false, false, false, 13, 13)
elment.dispatch(evt);

but failed to do in Chrome as Chrome doesn't support initKeyEvent, is there any workaround?

P.S: I want to press the enter key in the textarea.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Osama Xäwãñz
  • 437
  • 2
  • 8
  • 21
  • 1
    this might help https://stackoverflow.com/questions/42550341/react-trigger-onchange-if-input-value-is-changing-by-state/42554283#42554283 – Shubham Khatri Apr 16 '18 at 09:45
  • I'd use the event constructor: https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events – evolutionxbox Apr 16 '18 at 09:46
  • Possible duplicate of [React: trigger onChange if input value is changing by state?](https://stackoverflow.com/questions/42550341/react-trigger-onchange-if-input-value-is-changing-by-state) – evolutionxbox Apr 16 '18 at 09:46
  • I want to press Enter in the textarea not to update its value, so for that, I have to trigger onkeypress event. – Osama Xäwãñz Apr 16 '18 at 11:22

0 Answers0