9

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

ptheofan
  • 2,150
  • 3
  • 23
  • 36
  • For anyone looking for a good solution to this question, please see this post (https://stackoverflow.com/questions/51636083/jquery-get-the-value-of-cut) – Ryan Wilson Aug 01 '18 at 15:06

3 Answers3

10

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)

ori
  • 7,817
  • 1
  • 27
  • 31
0

I would suggest looking at this, JavaScript get clipboard data on paste event (Cross browser), it is for the paste event but I'm sure you could do something similar and compare the current value to that on the clipboard, if they are exactly the same than the input would be empty, otherwise not.

Community
  • 1
  • 1
ars265
  • 1,949
  • 3
  • 21
  • 37
0

Hope I got you right:

In jQuery you could use something like this to see if a textbox is empty on every user's keyup:

var txt;
$('#textbox_ID').live('keyup', function() {
    txt = $(this).val().length;
    if(txt < 1) {
        alert("textbox is empty");
    }
});

This should work, because everytime the user releases a key and has the textbox focused, it checks if it's empty.

Fabian
  • 3,465
  • 4
  • 34
  • 42
  • Nay, mate, I'm looking to detect if the textbox is empty during a cut event – ptheofan Feb 21 '12 at 07:51
  • It's not possible to get the clipboard data in every browser due to security reasons. Why do you want to check the textbox *during* the cut event? You mentioned your goal is to know if the user cut all the text, so it should be empty *after* the cut event? – Fabian Feb 21 '12 at 13:44
  • There are several questions on this topic on SO: http://stackoverflow.com/questions/1144184/js-get-clipboard-data – Fabian Feb 21 '12 at 13:50