1

I have a question about jquery.

I know we can detect copy with jquery like this :

$("#textA").bind('copy', function() {
    $('span').text('copy behaviour detected!')
}); 
$("#textA").bind('paste', function() {
    $('span').text('paste behaviour detected!')
}); 
$("#textA").bind('cut', function() {
    $('span').text('cut behaviour detected!')
});

But , i want to know what is exactly copied ?

for example, user copy the value of one box, i need to get and have that value .

How to do this with jquery?

Pap
  • 215
  • 2
  • 10
  • 1
    Something like this ? http://stackoverflow.com/questions/5379120/get-the-highlighted-selected-text – Joffrey Maheo Jun 06 '14 at 11:59
  • @JoffreyMaheo yes, thanks, but not with selecting , i need something like when user select and copy text , then for example that value saves in a variable and alert that ... – Pap Jun 06 '14 at 12:07
  • So use this function and return selected text when ctrl+C was pressed, like that http://stackoverflow.com/questions/4604057/jquery-keypress-ctrlc-or-some-combo-like-that – Joffrey Maheo Jun 06 '14 at 13:50

1 Answers1

3

You can use this

function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}

$(document).keypress("c",function(e) {
    if(e.ctrlKey)
    alert(getSelectionText());
});

this should be work

Joffrey Maheo
  • 2,919
  • 2
  • 20
  • 23