+ 2
[SOLVED] How to copy everything except a specific part of a paragraph in Javascript?
I have a paragraph which content is randomized but the last part of it isn't. How can I copy the paragraph without copying the last part what is not randomized? I have no idea, I just know that the "select()" selects the paragraph, "setSelectionRange()" sets the selection range (obviously) and the "document.execCommand('copy')" copies it. The problem is, that I can't set the selection range to the right length because the paragraph's length is almost always different because it is randomized. So how can it be done in Javascript? EDIT: I meant a textarea, not a paragraph. You can't copy a paragraph with document.execCommand('copy').
17 Respostas
+ 2
I'm thinking maybe just copy the paragraph content, then preserve only (length of paragraph content minus length of static part that isn't randomised) characters.
+ 1
Ipang How do I subtract it? I tried, but with no success. Am I just dumb lol
+ 1
Antti Lille
Share your code please, I'm not all that with JS, but maybe I can try something out.
Follow this guide to sharing your code link 👍
https://www.sololearn.com/post/75089/?ref=app
+ 1
Ipang Here you go, raw version of it👍
https://code.sololearn.com/WU4oA61IJ436/?ref=app
+ 1
Antti Lille
Good news! copy to clipboard works 👍
Now, about my idea, I was thinking that, if you know the length of the last part (that isn't randomized), then assuming total text length (textarea.value) be 200 character, and length of last part be 15, then you can subtract 200 by 15, and use that result as the selection range width.
+ 1
Ipang I tried to subtract like this: copyText.setSelectionRange(0, allContent.length - lastPart.length);
Simply it selects all the content minus the last part, pretty much the same way you did.
It works, but if I add "onClick", it doesn't work anymore (don't ask why, it'll be a hard and long thing to explain lol😅)...🙄 But I'll try to fix it on my own. Thanks a lot!😄
+ 1
Ipang Updated!👍 Took longer than should've because the code didn't work for some reason
+ 1
Ipang It works (at least it should work) in every device, but I have a code to my own website where if I add that code (what is in my code bits), the website's code doesn't work anymore. Hopefully you understood, it's hard to explain lol😅
+ 1
Oh okay I get it👌
+ 1
Ipang Thanks one more time!😄
+ 1
No problem bud! 👌
Wish I could do more, though I don't know your website coding 😁
+ 1
Ipang Currently I don't need help (anymore), but in the future for sure!😁😆 Wait a sec... Actually I got another problem in a beta-test on my website. It's just a test so it's not public. I am stuck with it lol😅 It's optional, I just tried to code something like it. I don't know how you can help, it's in Finnish so you don't understand anything from it lol, but I'll try to code something like it in English👍 It seems you want to help, so try to help me😄
+ 1
I think you better post that Finnish code on a new thread, wider audience, and skill ranges.
0
Antti Lille
Update the code with latest changes please, so I can check it too ...
0
Antti Lille
I checked it, in SoloLearn it works.
I copied the code, save it in my phone (Android), then open it in built-in browser & Chrome, it works too.
Is it not working in PC or iPhone or laptop?
0
Hmmm I think I'll pass it on for someone who speaks Finnish instead then 😁
0
Ipang I could do an example of it in English if you want. It'll take some time, though lol