0

I have searched a lot and saw plenty of answers but none solved my problem. I am making a TamperMonkey extension for a certain website. All I want to do is to have a behavior that literally presses a key for eg. 'A' then press 'BackSpace' to delete the value again.

First of all, I want to solve the problem of pressing any letter.

I don't want to use $('.classDiv').append('A'); because it doesn't appear as if a keyboard button was pressed. So I have used all the codes in the references below even understanding the difference between codes:

Maybe the following will give us a hint, the div shows multiple event handlers using FireFox:

  • click
  • onBeforeInput
  • onBlur
  • onClick
  • onCompositionStart
  • onCompositionEnd
  • onFocus
  • onInput
  • onKeyDown
  • onKeyPress
  • onPaste
  • onSelect

Note: the div serves as text input.

Joe
  • 879
  • 3
  • 14
  • 37
  • 1
    With a `contenteditable` element you can simply focus it and do execCommand like document.querySelector('[contenteditable]').focus(); document.execCommand('insertText', false, 'a'); – wOxxOm Aug 08 '19 at 18:23
  • @wOxxOm hehe, it worked. I don't know about `execCommand` but it seems a form of command functions. – Joe Aug 08 '19 at 19:01
  • @wOxxOm so I now added the letter 'a'. Is there a similar way to delete this letter?? – Joe Aug 08 '19 at 19:32
  • Similarly focus the element then getSelection().modify('extend', 'backward', 'character'); document.execCommand('delete') – wOxxOm Aug 09 '19 at 04:39

0 Answers0