0

I tried to use the event API, such as paste.

In my JavaScript code, it does not work.

when the user copy text from form,how can I get the selected value?

UPDATE

I use following code and it works.

function handlePaste(e) {
  var clipboardData, pastedData;
  // Stop data actually being pasted into div 
  e.stopPropagation();
  e.preventDefault();
  // Get pasted data via clipboard API 
  clipboardData = e.clipboardData || window.clipboardData;
  pastedData = clipboardData.getData('Text');
  // Do whatever with pasteddata 
  alert(pastedData);
}
document.getElementById('editableDiv').addEventListener('paste', handlePaste);
Community
  • 1
  • 1
GraceKola
  • 1
  • 2
  • 1
    can you please share your code – brk Apr 20 '17 at 07:54
  • [This could help you achieve what you want](http://stackoverflow.com/questions/2176861/javascript-get-clipboard-data-on-paste-event-cross-browser) – ABEL MASILA Apr 20 '17 at 07:56
  • If you want to prevent the paste from happening, using an oninput might not work very well. – Charlie Apr 20 '17 at 08:08
  • thanks for helping. I use following code and it works. function handlePaste (e) { var clipboardData, pastedData; // Stop data actually being pasted into div e.stopPropagation(); e.preventDefault(); // Get pasted data via clipboard API clipboardData = e.clipboardData || window.clipboardData; pastedData = clipboardData.getData('Text'); // Do whatever with pasteddata alert(pastedData); } document.getElementById('editableDiv').addEventListener('paste', handlePaste); – GraceKola Apr 20 '17 at 08:33

0 Answers0