Jump to content

Dr Pepper Can

Members
  • Posts

    3
  • Joined

  • Last visited

Posts posted by Dr Pepper Can

  1. JMRKER was kind enough to improvise this solution below. However, although I need to have information (text) copied to the clipboard, I need NOT to have it in a textarea. It shouldn't be available for modification.
    Isn't there a way to keep this text data in the code, and then transferring it to the clipboard when a button is clicked?

    Thanks guys :)

     

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" /> <title> HTML5 page </title>
    <!-- From: https://stackoverflow.com/questions/6300213/copy-selected-text-to-the-clipboard-without-using-flash-must-be-cross-browser -->
       <style> </style>
    </head>

    <body>
    <textarea id="textarea" rows="6" cols="40">
        Lorem ipsum dolor sit amet, eamsemper maiestatis no.
    </textarea><br/>
    <button id="copyTAreaBlock">Click to copy</button>
    <span id="copyTAreaAnswer"></span>
    <p> <input type="text" id="textInput"><br/>
    <button id="copyText">Click to copy</button>
    <span id="copyTextAnswer"></span>

    <script> // Setup the variables for TEXTAREA
    var textarea = document.getElementById("textarea");
    var answer = document.getElementById("copyTAreaAnswer");
    var copyTArea = document.getElementById("copyTAreaBlock");

    copyTArea.addEventListener('click', function(e) {
    // Select some text (you could also create a range) textarea.select();
    // Use try & catch for unsupported browser try {
    // The important part (copy selected text)
    var ok = document.execCommand('copy');

    if (ok) answer.innerHTML = 'Copied!';
    else answer.innerHTML = 'Unable to copy!';
    } catch (err) {
    answer.innerHTML = 'Unsupported Browser!'; } });

    </script>
    </body>

    </html>

×
×
  • Create New...