I want to fire from a button Ctrl+V and Ctrl+Z to a targeted textarea. If it is possibility to have this as an onclick
button instead of id
button that would be perfect. How do I do that?
NOTE: Please do not misunderstand this post with detecting or alert keypress. I do NOT want that. I want to have a button that executes this keypress onto the textarea.
My JavaScript:
<script type="text/javascript">
var t = document.getElementById('mytextarea'),
bcsv = document.getElementById('bcv'),
bsz = document.getElementById('bcz'),
cv = document.createEvent('KeyboardEvents'),
cz = document.createEvent('KeyboardEvents');
cv.initKeyboardEvent(
'keydown',
true, // key down events bubble
true, // and they can be cancelled
document.defaultView, // Use the default view
true, // ctrl
false, // alt
false, //shift
false, //meta key
86, // keycode
0
);
cz.initKeyboardEvent(
'keydown',
true, // key down events bubble
true, // and they can be cancelled
document.defaultView, // Use the default view
true, // ctrl
false, // alt
false, //shift
false, //meta key
90, // keycode
0
);
bcz.addEventListener('click', function(){
t.dispatchEvent(cz);
}, false);
bcsz.addEventListener('click', function(){
t.dispatchEvent(csz);
}, false);
</script>
My HTML:
<input type="button" value="CTRL+V" id="bcv" />
<input type="button" value="CTRL+Z" id="bcz" />
<textarea id="mytextarea" rows="5" cols="10"></textarea>
Question: I never managed to make this work. So how do I make it work? Or is it another way to make this work? How do I make two buttons to act as if I would push Ctrl+V and Ctrl+Z on the keyboard on a targeted textarea? But without using the keyboard.