I have a textarea and a button. Clicking the button causes text to be inserted into the textarea.
Is there a way to allow a user to press Ctrl/Cmd+z to undo the inse
Add html first and you can use keypress event for undo
you can try here also here http://jsfiddle.net/surendra786/1v5jxaa0/
ADD
UNDO
**then add jquery **
var items = [];
$("#add").click(function() {
// Push the new actor in the array
items.push($("[name='actor']").val());
populate();
});
$(document).keydown(function(e){
if( e.which === 90 && e.ctrlKey ){
console.log('control + z');
if (items.length > 0) {
// remove last element of the array
items.splice(-1,1);
populate();
}
}
});
populate = function() {
$("[name='actors-list']").text('');
$("[name='actors-list']").append(items.join('
'));
$("[name='actors']").val(items.join(','));
}
you can try here http://jsfiddle.net/surendra786/1v5jxaa0/
this is working for me