0

Could anyone tell how to select portion of text in textarea and copy it to clipboard by using javascript? I know how to select all text in textarea and copy it? My question is that when we use mouse to select part of text in textarea, how to copy it to clipboard.

Andy Tang
  • 7
  • 4
  • Here's a detailed answer around copying text via javascript: https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript – Varinder Feb 16 '18 at 01:02
  • Varinder, I appreciate your help. I saw that, however, it only selects all texts in box. – Andy Tang Feb 16 '18 at 01:05
  • Possible answer: https://stackoverflow.com/questions/22530738/how-to-execute-ctrlc-or-copy-commad-using-javascript-or-jquery-on-button-click Please visit this link for more information: https://developer.mozilla.org/en-US/docs/Web/API/Range – cjatstackoverflow Feb 16 '18 at 02:00

1 Answers1

0

How about this

(on)select save the start and end position of the selection to the input element itself.

event.target.lastSelection = { start:startpos, end:endpos};

and use the (on)blur event to do something like

event.target.setSelectionRange( event.target.lastSelection.start, event.target.lastSelection.end )?

Raj Josyula
  • 94
  • 11