Jump to content

Create a "copy <HTML code> to clipboard" button

Recommended Posts

Site URL: https://www.capemademark.org/use-the-mark

Hi, I would like to create a button that copies HTML code to your clipboard for this page on my website: https://www.capemademark.org/use-the-mark

Visitors to the page must be able to copy the HTML code and paste it into code blocks on their own websites. 

Here is the solution to copy TEXT to clipboard:

<p>Click copy to clipboard</p>

<input type="text" value="Hello World" id="myInput">
<button onclick="myFunction()">Copy text</button>

<script>
function myFunction() {
  var copyText = document.getElementById("myInput");
  copyText.select();
  copyText.setSelectionRange(0, 99999)
  document.execCommand("copy");
  alert("Copied the text: " + copyText.value);
}
</script>

 

Link to comment
  • 1 month later...
  • Replies 1
  • Views 1.8k
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

On 5/28/2022 at 9:29 PM, whattingh said:

Site URL: https://www.capemademark.org/use-the-mark

Hi, I would like to create a button that copies HTML code to your clipboard for this page on my website: https://www.capemademark.org/use-the-mark

Visitors to the page must be able to copy the HTML code and paste it into code blocks on their own websites. 

Here is the solution to copy TEXT to clipboard:

<p>Click copy to clipboard</p>

<input type="text" value="Hello World" id="myInput">
<button onclick="myFunction()">Copy text</button>

<script>
function myFunction() {
  var copyText = document.getElementById("myInput");
  copyText.select();
  copyText.setSelectionRange(0, 99999)
  document.execCommand("copy");
  alert("Copied the text: " + copyText.value);
}
</script>

 

document.execCommand is deprecated at the moment. Kindly refer the navigator.clipboard API 

BeyondSpace - Squarespace Website Developer

🖼️ Lightbox Studio (Enable Pinch/Zoom on lightbox)
🗓️ Delivery Date Picker (Squarespace Date picker form field)
💫 Gallery block 7.1 workaround
🥳 Sparkplugin Customisations Browsers (Browse +100 Spark plugin customisations)
🥳 Elfsight Template Browsers (Browse +1000 Elfsight widget Templates)

If you find my answer useful, let's leave a like or upvote so others with the same issue can find their solution. Thank you!

 

Link to comment

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.