How to copy plain text value from using Javascript -


function copytext(text) {     var textfield = document.createelement('textarea');     textfield.innertext = text;     document.body.appendchild(textfield);     textfield.select();     document.execcommand('copy');     textfield.remove(); } 

i found code on reddit , thought work logical create element select after execute command 'copy'. surprised didn't , not know why.

no error given when running script on chrome dev console want execute , that's why don't want hear answer any api has copying. if can tell me how use api on chrome dev tools feel free let me know that.

if there things left out or have questions about.

the code wont work without user interaction, if try run console, not work.

the way run code bind function button or that.

function copytext(text) {      var textfield = document.createelement('textarea');      textfield.innertext = text;      document.body.appendchild(textfield);      textfield.select();      document.execcommand('copy');      textfield.remove();    }
<button onclick='copytext("some text")'>copy text!</button>

you can try calling document.querycommandsupported("copy") or document.querycommandenabled("copy") dev console buttons verify this.

google developers

see known bugs section @ bottom.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -