0

I would like to simulate that there was pressed backspace key on keyboard after i click on button but all my attempts have failed. Here is my code:

function rtf(){ 

document.getElementById("u0u").focus();
$('#u0u').trigger(jQuery.Event('keypress', { keycode: 8 }));
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id='u0u'>
<div onclick='rtf()' style='cursor:pointer'><span>Backspace</span></div>
rootz
  • 1
  • 7
  • Possible duplicate of [Definitive way to trigger keypress events with jQuery](http://stackoverflow.com/questions/832059/definitive-way-to-trigger-keypress-events-with-jquery) – Adam Azad Jan 15 '17 at 16:52
  • @AdamAzad i must be doing something wrong. I have tried 3 of the solutions from this thread and anything does not delete the text in my input. could you please correct my code? – rootz Jan 15 '17 at 17:10
  • Could you post the handle for keypress, please? – Adam Azad Jan 15 '17 at 17:15
  • why do you want specifically simulate the key press? are you expecting to call a function that it attach to that event? – ncubica Jan 15 '17 at 17:26
  • @ncubica Yes. I have no access to that function becouse it is somewhere deep in tinymce source code – rootz Jan 15 '17 at 17:38
  • would be better to take a look if you have access to that function over the `tinymce` object? any way I think this is what you are looking for http://output.jsbin.com/awenaq/4 – ncubica Jan 15 '17 at 17:45
  • @ncubica thank you it looks pretty useful but i can not figure out how to use it to write in a textfield. i tried to focus at the input before the function press a key but it does nothing – rootz Jan 16 '17 at 20:20

1 Answers1

0

Forget about manually triggering the keypress/keyup event, they don't work anymore due to security rules enforced by new browser, the only way that I've found working to simulate keystrokes is using the Sendkeys Jquery Plugin , it gets around the browser's restriction so it's more like a hack but certainly working as simple as :

$('#u0u').sendkeys("this will be simulated");

If you want to simulate the backspace key (keycode : 8), use it like this :

$('#u0u').sendkeys("{backspace}");
mrbm
  • 2,164
  • 12
  • 11