Jump to content

Custom code for copying textbox

Recommended Posts

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
  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.