The code below does three things...
- focuses a text box when it detects the Ctrl + V key press (paste function);
- pastes in that box whatever text content is in the clipboard;
- lastly, it submits the web form by clicking the submit button.
It works good, with the exception that the JavaScript does not send out the pasted text to the input box variable, as I would like it to do. That variable is empty after submission. You can see that in the URL ("variable="), if you execute the code in your browser.
In short, when I press Ctrl + V, I would like the script to automatically paste and submit whatever text content is in the clipboard.
Can you tell me how to fix this, please?
document.addEventListener('keydown', function(event) {
if (event.ctrlKey && (event.keyCode == 86)) { // Ctrl + V
document.getElementById('box').focus();
document.getElementById('button').click();
}
});
<form>
<input id='box' name='variable'>
<input id='button' type='submit'>
</form>