0

I have been creating a profile making program and I would like to use the output of this JS function as a simple string text inside the textarea box which the user can copy using the "Copy to Clipboard" button. Any ideas, please let me know ASAP. Here is my current code:

<!--HTML-->
<!--Button Code-->
<button onclick="copyprofile()">Copy to clipboard</button><br>   
<!--Textarea Code-->
<textarea id="text" cols="60" rows="13">I want output of createProfile() to be here</textarea><br>

//Javascript
function createProfile(){
    var fn,ln,a,fc,fs,ff,profile;
    fn = prompt("Please enter your first name","Enter first name here");
    ln = prompt("Please enter your last name","Enter last name here");
    a = prompt ("Please enter your age","Enter age here");
    fc = prompt("Please enter your favourite colour","Enter favourite color here");     
    fs = prompt("Please enter your favourite sport","Enter favourite sport here");
    ff = prompt("Please enter your favourite food","Enter favourite food here");
    profile = ("Name: " + fn + " " + ln + "<br>Age: " + a + "<br>Favourite colour: " + fc + "<br>Favourite sport: " + fs + "<br>Favourite food: " + ff);
    return profile;
}
function copyProfile(){
    var text = document.getElementById('text');
    var range = document.createrange();

    range.selectNode(text);
    window.getSelection().addRange(range);
    document.execCommand(copy);
}

If you have any thoughts or ideas on how to achieve this, please let me know

EnigmaRM
  • 7,523
  • 11
  • 46
  • 72
Archit123
  • 5
  • 5

1 Answers1

1

Being that you have already saved everything in a string assigned to profile, just reference the element you want to update and assign it as the value.

In javascript:

document.getElementById("text").value = profile;

And if you want the to keep the line breaks, you'll need to do something other than <br> as a textarea doesn't typically render HTML. I'd suggest doing a carriage return or line feed. \n

profile =  ("Name: " + fn + " " + ln + "\nAge: " + a + "\nFavourite colour: " + fc + "\nFavourite sport: " + fs + "\nFavourite food: " + ff);;

Here is a JSFiddle with an example https://jsfiddle.net/x1w1t8ux/


Update

I took some time to look at the rest of your code as you said it still was not working as you expected. Your copyProfile function has a couple of errors. If you open up your developer console when trying to run these functions, you'll see the error messages:

Uncaught TypeError: document.createrange is not a function

You're line document.createrange() is not a function. You need to camel case it to be document.createRange().

After you fix that error, try to run the code again will display another error in the console:

Uncaught ReferenceError: copy is not defined

In this line document.execCommand(copy); you are referencing a variable called copy. That variable does not exist, nor is it what you're looking for. You are wanting to pass the string copy to the function execCommand. It should look like document.execCommand('copy'); (with quotes, as that's how you identify a string in JavaScript.

In your HTML, when you click on your Copy To Clipboard button <button onclick="copyProfile()">Copy to clipboard</button> it throws an error

Uncaught ReferenceError: copyprofile is not defined

You do not have a function called copyprofile, you have one called copyProfile. Function names are case sensitive. I would recommend sticking to a consistent naming convention (such as camel case)

Lastly, no where in your code are you calling the function createProfile(). So I created it as a second button in my testing.

Community
  • 1
  • 1
EnigmaRM
  • 7,523
  • 11
  • 46
  • 72
  • Thanks mate. Appreciate the help! – Archit123 Jun 02 '16 at 18:41
  • No problem. if it solved your problem, go ahead and mark as accepted answer to help others in the future as well. – EnigmaRM Jun 02 '16 at 18:49
  • Hi mate, just wondering if you have any idea of what to put inside the actual – Archit123 Jun 02 '16 at 18:49
  • Sorry, I am a new programmer and this is how I am trying to run my code: I have been trying to use: profile = ("Name: " + fn + " " + ln + "\nAge: " + a + "\nFavourite colour: " + fc + "\nFavourite sport: " + fs + "\nFavourite food: " + ff); document.getElementById('text').value return: profile;
                 But this has not been working. Any ideas?
    – Archit123 Jun 02 '16 at 18:57
  • Looks like you're missing assigning `profile` to the element's `value`. `document.getElementById("text").value = profile;` – EnigmaRM Jun 02 '16 at 19:03
  • Cheers again Enigma. Just what I was looking for. You are a genius. Thanks again – Archit123 Jun 02 '16 at 22:00
  • Is there any way to achieve the user inputting their profile info from a prompt rather than the input being fixed in the text editor by the creator? If so, please let me know – Archit123 Jun 03 '16 at 10:09
  • Not really sure what you're asking. Are you referring to the example I provided in jsfiddle? I hardcoded those values for testing so that I wouldn't have to go through the process of answering everything. You can continue to use your existing code. The original answer I gave you was intended to be added to your existing code... – EnigmaRM Jun 03 '16 at 15:08
  • Sorry for any confusion caused. The code provided in this link: http://postimg.org/image/7w6duuizf/ was not working so just wondering if you could help. Thanks – Archit123 Jun 03 '16 at 15:40
  • But what specifically is not working? Are you getting errors in your console? Is the copy to clipboard not working? is the user input not being displayed in the `textarea`? – EnigmaRM Jun 03 '16 at 15:55
  • You're in luck. I took the time to troubleshoot your other function. FYI, typically this requires asking a new question. Not just asking for additional help in the comments. – EnigmaRM Jun 03 '16 at 16:06
  • I'm also going to add, make sure you're taking the time to understand what this code is doing. It appears that you're copying & pasting others' answers. – EnigmaRM Jun 03 '16 at 16:06
  • The actual prompts are not appearing to ask the user their profile inputs. – Archit123 Jun 03 '16 at 16:20
  • See my updated answer. It's because no where are you calling the function `createProfile()`. Add another button to call it. – EnigmaRM Jun 03 '16 at 16:47
  • Do you mean create another button to call `createProfile()` as in this?: `
    `
    – Archit123 Jun 03 '16 at 18:17
  • Also what should I set my var `copy` equal to? – Archit123 Jun 03 '16 at 18:19
  • Did you read the link I posted for `execCommand()`? You are supposed to pass in the string copy. `execCommand('copy')`. – EnigmaRM Jun 03 '16 at 18:20
  • I have literally given you step-by-step instructions that you can copy and paste to get your code working. Just take the time to read what has been posted in it's entirety! – EnigmaRM Jun 03 '16 at 18:21
  • Thanks so so much Mr Enigma. The code worked perfectly after thoroughly researching and understanding this subject. I have now styled it and added more questions and all the code is working perfectly. You can see how it looks if you like by copying code from http://hostcode.sourceforge.net/view/5950. Thanks again :) – Archit123 Jun 04 '16 at 17:46