2

I have a code like this below, but when I want to copy the selected text from textarea by using document.execCommand("copy"); it doesn't copy to clipboard I search a lot and none of found solutions worked for me How can I copy the selected text?

Sample Code:

data.forEach(function( single ) {
        result += '^' + fontSize + ' ' + single.sno + '\n';
});
var $temp = $("<textarea></textarea>");
$("body").append($temp);
$temp.val(result+"").select();
document.execCommand("copy");
$temp.remove();
alert('copied');
wahdan
  • 1,208
  • 3
  • 16
  • 26
Mohammad
  • 69
  • 10

1 Answers1

1

You Don't need all that code you have written

$("button").click(function(){
    $("textarea").select();
    document.execCommand('copy');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Copy</button>
<br/>
<textarea></textarea>
wahdan
  • 1,208
  • 3
  • 16
  • 26