+ 3

[SOLVED] Copy

Im trying to make it so when u click the button it copys the text, it has the command for it but its giving me errors and i dont know what to do. Im not sure if its becasue on w3schools they say to not use the command or something else https://code.sololearn.com/WhIa55cHV8Uu/?ref=app

17th May 2022, 1:01 AM
Junior
Junior - avatar
20 Antworten
+ 5
Your Mom did you remove the line which selects the text document.getElementById("cop").select(); This one you have to remove it
17th May 2022, 1:24 PM
YUGRAJ
+ 2
You should write document.execCommand
17th May 2022, 1:30 AM
YUGRAJ
+ 2
function my(){ document.getElementById("cop").select(); document.execCommand("copy") } execCommand('copy') copies the selection to the clipboard, the selection is made using the select() method.
17th May 2022, 1:32 AM
Solo
Solo - avatar
+ 2
Your Mom execCommand is a function of global object document not an object of element that's why you get error. when you do document.getElementById('cop'). execCommand ('copy') it just copy once means when you try to edit textbox then do copy then it won't copy edited text. It just show you previous copied text.
17th May 2022, 4:58 AM
A͢J
A͢J - avatar
+ 1
Did you select it before pressing button
17th May 2022, 1:28 PM
YUGRAJ
+ 1
It is working for me
17th May 2022, 1:29 PM
YUGRAJ
+ 1
Your Mom You need to select text before copying function my() { var ele = document.getElementById('cop'); ele.select(); document.execCommand("copy"); }
17th May 2022, 1:45 PM
A͢J
A͢J - avatar
0
why is it like that? any reason is it just built in like that?
17th May 2022, 1:31 AM
Junior
Junior - avatar
0
Solo ,YUGRAJ why do you not put document.getElementById…? forthr docuemnt.execCommand. Is it built like that or is there another reason?
17th May 2022, 1:35 AM
Junior
Junior - avatar
0
yes but whenever i use document.getElementById(“cop”).select(); document.getElementById(“cop”).execCommand(“copy”); it comes out as an error, but whenever i use document.getElementById(“cop”).select(); document.execCommand(“copy”); it works. why is that? Shouldnt they both perform the same task?
17th May 2022, 2:05 AM
Junior
Junior - avatar
0
k so one last thing, how do i make it so it copys text that the user selected? I tried adding a global variable to my select() and applying that to my execCommand() but it didnt work
17th May 2022, 12:51 PM
Junior
Junior - avatar
0
function my(){ document.execCommand("copy") }
17th May 2022, 1:08 PM
Solo
Solo - avatar
0
yes
17th May 2022, 1:28 PM
Junior
Junior - avatar
0
idk if its because im on mobile or not
17th May 2022, 1:29 PM
Junior
Junior - avatar
0
r u on computer?
17th May 2022, 1:29 PM
Junior
Junior - avatar
0
I am using in mobile
17th May 2022, 1:29 PM
YUGRAJ
0
ok, ill try again later
17th May 2022, 1:32 PM
Junior
Junior - avatar
0
I am on mobile and its working.
18th May 2022, 10:23 PM
Gazloco
Gazloco - avatar
- 1
Solo That selects all text in textarea i just want it to copy what user wants. For example if i have Hello World! written in textarea and i only want Hello to be copied i would select it and press the button. idk how to do that and i tried multiple ways
17th May 2022, 1:14 PM
Junior
Junior - avatar
- 1
YUGRAJ i removed the line but then it doesnt copy anything
17th May 2022, 1:27 PM
Junior
Junior - avatar