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++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -