[SOLVED] How to copy everything except a specific part of a paragraph in Javascript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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').

1st Oct 2020, 2:23 PM
Antti L 🇫🇮
Antti L 🇫🇮 - avatar
17 Answers
+ 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.
1st Oct 2020, 2:33 PM
Ipang
+ 1
Ipang How do I subtract it? I tried, but with no success. Am I just dumb lol
1st Oct 2020, 4:42 PM
Antti L 🇫🇮
Antti L 🇫🇮 - avatar
+ 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
2nd Oct 2020, 11:18 AM
Ipang
+ 1
Ipang Here you go, raw version of it👍 https://code.sololearn.com/WU4oA61IJ436/?ref=app
2nd Oct 2020, 11:51 AM
Antti L 🇫🇮
Antti L 🇫🇮 - avatar
+ 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.
2nd Oct 2020, 1:55 PM
Ipang
+ 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!😄
2nd Oct 2020, 4:38 PM
Antti L 🇫🇮
Antti L 🇫🇮 - avatar
+ 1
Ipang Updated!👍 Took longer than should've because the code didn't work for some reason
2nd Oct 2020, 5:47 PM
Antti L 🇫🇮
Antti L 🇫🇮 - avatar
+ 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😅
3rd Oct 2020, 6:42 AM
Antti L 🇫🇮
Antti L 🇫🇮 - avatar
+ 1
Oh okay I get it👌
3rd Oct 2020, 6:53 AM
Ipang
+ 1
Ipang Thanks one more time!😄
3rd Oct 2020, 6:55 AM
Antti L 🇫🇮
Antti L 🇫🇮 - avatar
+ 1
No problem bud! 👌 Wish I could do more, though I don't know your website coding 😁
3rd Oct 2020, 6:57 AM
Ipang
+ 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😄
3rd Oct 2020, 7:17 AM
Antti L 🇫🇮
Antti L 🇫🇮 - avatar
+ 1
I think you better post that Finnish code on a new thread, wider audience, and skill ranges.
3rd Oct 2020, 7:27 AM
Ipang
0
Antti Lille Update the code with latest changes please, so I can check it too ...
2nd Oct 2020, 4:46 PM
Ipang
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?
2nd Oct 2020, 10:08 PM
Ipang
0
Hmmm I think I'll pass it on for someone who speaks Finnish instead then 😁
3rd Oct 2020, 7:20 AM
Ipang
0
Ipang I could do an example of it in English if you want. It'll take some time, though lol
3rd Oct 2020, 7:23 AM
Antti L 🇫🇮
Antti L 🇫🇮 - avatar