0

I am trying to display whatever a user copies when using IE. Im using jquery and the following code:

$('#data').bind("copy", function(e) {alert(clipboardData.getData("text"));})

The problem is whenever I copy it shows the data previously copied. not the current data showing the function is called before the data is put on the clip board. is there any workaround

SNAG
  • 2,011
  • 2
  • 23
  • 43

1 Answers1

1

I think what you want is to get the selected text. See below code to get the text selection,

DEMO

$('#data').bind("copy", function(e) {
    var selectedText;
    //reference post: https://stackoverflow.com/a/275825/297641
    // IE version
    if (document.selection != undefined) {
        $(this).focus();
        var sel = document.selection.createRange();
        selectedText = sel.text;
    }
    // Mozilla version
    else if (textComponent.selectionStart != undefined) {
        var startPos = textComponent.selectionStart;
        var endPos = textComponent.selectionEnd;
        selectedText = textComponent.value.substring(startPos, endPos)
    }

    alert(selectedText);
});

Reference: How to get selected text from textbox control with javascript

Community
  • 1
  • 1
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134