Forum Discussion

MaryFranchetti's avatar
MaryFranchetti
Community Member
8 months ago

Java script for multiple copy buttons not working

I have multiple slides. On slide one for all the copy buttons I am using the following and just changing the text in between the " " on the copyFunction line, This is working perfectly. However, when I go to slide 2 and use the same java script for the layers on that slide, it does not copy the text. Can anyone give me a suggestion on that I am doing wrong?
var player = GetPlayer();
copyFunction ("I won't be able to help you recover this account, but I can help you create a new Account with a different email address. Are you interested in doing that today?");
function copyFunction(tt) {
  
  const copyText = tt;
  const textArea = document.createElement('textarea');
  textArea.textContent = copyText;
  document.body.append(textArea);
  textArea.select();
  document.execCommand("copy");
}

  • JS is notoriously finicky, so it is tough to tell without seeing the project. The tiniest thing missed in copying can throw it off. It is also notorious for distinguishing between straight and curly quotation marks, so you might check that.

    Attach your .story file here, and someone can help; there are a lot of strong programmers that frequent this forum.

    I do have one suggestion. It seems to me that you want a customized message to appear on different layers or slides when certain conditions are met. I can two ways of doing that that seem easier to me than using JS. (It should be noted that for me, JS is an absolute very last resort, because the next person that has to maintain the project may not have a programming background.)

    If you place a text area on a slide, and put nothing in it except a text variable reference, nothing will show as long as the variable is blank. So instead of your trigger executing JS, it can change the variable, and it will suddenly appear. (Setting it back to blank will make it disappear.)

    If you want more in the message than just plain text, you might consider a lightbox slide. Again, using a text variable reference will allow the message to change to match the conditions. It will also allow you to put other things (like the Yes No buttons to answer the question) on it. Using a lightbox also frees you from designing around where the text area might show up, and you have only one design.

  • Thank you for your response. Sadly, I am trying to make it so the text can be quickly copied with a button, which is why I am using JS. 

  • I think there are easier methods, (if not as glamorous), of doing what you describe, if I am reading your script and posts correctly.

    The slides in the attached sample show using variables, states, layers, or light boxes to do what you describe. If none of these accomplish what you want, I suggest you upload your .story file. Then someone might be able to solve the issue.

    Here are the best practices for uploading a .story file:

    • Only include slides that are related to the problem.
    • Be sure objects, layers, motion paths, and variables have meaningful names.
    • If there is proprietary content, replace or delete it. For example, replace proprietary text with “ipsum lorem” text.
    • Be sure to clearly describe the following in the file and/or in the post with the attachment (if you haven’t already done so):
      • What you want to happen
      • What is not happening that should happen
      • What is happening that should not happen
      • What you have already tried

    Sorry, file attached now.

  • Thank you - Your post seemed to indicate you uploaded a sample. If I read that correctly, I am not seeing the upload.

  • Thanks for the upload. How can I make it so that when you show the text it can be copied to the clipboard? (for example, the text shown when you click "can't help")

  • Nedim's avatar
    Nedim
    Community Member

    This is how I'd do it:



    var text = getVar("HelpMessage");
    copyFunction (text);

    function copyFunction(tt) {
      const copyText = tt;
      const textArea = document.createElement('textarea');
      textArea.textContent = copyText;
      document.body.append(textArea);
      textArea.select();
      document.execCommand("copy");
    }