LewisC Posted October 19, 2022 Share Posted October 19, 2022 We want to have a button where a user can copy text to clipboard from a pre-filled textbox. Using the code below almost works buy when clicking the button, no text is copied. Any suggestions would be appreciated! <html> <body> <center> <input type="text" value="#example #example #example" id="myInput"> <button onclick="myFunction()">Copy Social Tags</button> <script> function myFunction() { // Get the text field var copyText = document.getElementById("myInput"); // Select the text field copyText.select(); copyText.setSelectionRange(0, 99999); // For mobile devices // Copy the text inside the text field navigator.clipboard.writeText(copyText.value); // Alert the copied text alert("Copied the text: " + copyText.value); } </script> </body> </html> Link to comment
joseph81 Posted October 19, 2022 Share Posted October 19, 2022 (edited) Well it works for me, what browser did you use? Some browsers like Edge might block clipboard access. This article tackles that problem: https://web.dev/async-clipboard/ Edited October 19, 2022 by joseph81 Jozsef Kerekes - Front-end developer and Squarespace enthusiast My Blog: https://ui-workarounds.comIf you like my answer, please give me an upvote/like. Highly appreciated. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment