Copy Text Using JavaScript Using document.execCommand()
Here is a Sample Code - Click Here To Copy
function copy(selector,command){
if(selector) document.querySelector(selector).select();
if(!command)command = 'copy';
document.execCommand(command);
}
Selector is for the querySelector of your Element like
You have to copy a Text Under Element with id report so you have to pass #report here.
Run This Function ...
copy('#report')
Here Second Option is Optional .
Possible Values :- 1. Copy, Cut
Default Value :- Copy.
See Demo Codes Here :- https://replit.com/@SH20RAJ/Tutorials#copy-text-js/index.html
See Demo :- https://tutorials.sh20raj.repl.co/copy-text-js/
Chats During Video Making :- https://simp.ly/p/vPDdmj
Tips :-
- See Example Code Here For Input and Copy Button.
function copy(selector,command){
if(selector) document.querySelector(selector).select();
if(!command)command = 'copy';
document.execCommand(command);
}
- Use User Select to Select the inner Text of Div. and Copy() for Copying After Select.
<pre>
<code id="code1" onclick="copy()" style="user-select: all;">
function copy(selector,command){
if(selector) document.querySelector(selector).select();
if(!command)command = 'copy';
document.execCommand(command);
}
</code>
</pre>
Comments