Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the value of a textbox during cut event

I have trapped the cut event (jquery) on a textbox. What I want is to get the text on the textbox during the cut event is triggered.

I've tried accessing the data the user cut via evt.originalEvent.clipboardData.getData('text') but returns undefined.

My goal is to know if the user cut all the text (textbox is now empty) or not.

Thanks in advance

like image 833
ptheofan Avatar asked Feb 20 '12 16:02

ptheofan


1 Answers

You can setTimeout with a duration of 0, which schedules a function for immediate execution. The nice thing is that the function will execute once the text has already been cut, so you can check then if your textarea is empty (which would mean that the user has cut all the text):

var ta = $('#YOUR_TEXTAREA');
ta.bind('cut', function() {
    setTimeout(function(){
        if (!ta.val()) { 
            // user cut the whole text.
        }
    },0);
});

You might also want to add a check before the setTimeout to test whether there is any text in the textarea before the text gets cut (if the user presses Ctrl^X without any text being selected, the cut event still triggers)

like image 89
ori Avatar answered Oct 18 '22 02:10

ori