Dr Pepper Can Posted November 17, 2017 Share Posted November 17, 2017 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> Link to comment Share on other sites More sharing options...
Ingolme Posted November 17, 2017 Share Posted November 17, 2017 The easiest solution would be to set the textarea to readonly by giving it a readonly attribute. You can style it to not look like a text area by giving it a transparent background and no border. If you actually want to copy text from an element that's not a textarea I would actually have to do some tests to see what the browsers do and don't allow. Link to comment Share on other sites More sharing options...
Dr Pepper Can Posted November 17, 2017 Author Share Posted November 17, 2017 Thanks for the ideas! It sounds very promising. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 20, 2017 Share Posted November 20, 2017 This forum offers a copy button when you select text. If the user selects the text themselves then you don't have to do anything, you can just tell the browser to copy it, the hard part is getting the text selected. Link to comment Share on other sites More sharing options...
Chikwado Posted November 22, 2017 Share Posted November 22, 2017 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now