[SOLVED] Copy | Sololearn: Learn to code for FREE!

+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

5/17/2022 1:01:08 AM

Your Mom

20 Answers

New Answer

+4

Your Mom did you remove the line which selects the text document.getElementById("cop").select(); This one you have to remove it

+2

You should write document.execCommand

+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.

+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.

+1

Did you select it before pressing button

+1

It is working for me

+1

Your Mom You need to select text before copying function my() { var ele = document.getElementById('cop'); ele.select(); document.execCommand("copy"); }

0

why is it like that? any reason is it just built in like that?

0

Solo ,YUGRAJ why do you not put document.getElementById…? forthr docuemnt.execCommand. Is it built like that or is there another reason?

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?

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

0

function my(){ document.execCommand("copy") }

0

0

idk if its because im on mobile or not

0

r u on computer?

0

I am using in mobile

0

ok, ill try again later

0

I am on mobile and its working.

-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

-1

YUGRAJ i removed the line but then it doesnt copy anything