By clicking “Accept All Cookies”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. View our Privacy Policy for more information.
How to Add a Code Snippet in Webflow CMS with Copy to Clipboard Functionality

How to Add a Code Snippet in Webflow CMS with Copy to Clipboard Functionality

5
min read
Published on

Adding code snippets to your Webflow blog can enhance the value of your content, especially for tutorials and technical guides. This article will walk you through the steps to embed a code snippet in your Webflow blog with a copy-to-clipboard functionality.

Step 1: Embed the Code in Webflow

  1. Open Your Webflow Project: Log in to your Webflow account and open the project where you want to add the code snippet.
  1. Add a Rich Text Element: Drag and drop a Rich Text element to the desired location on your Webflow CMS page.
  1. Enter the Embed Code Mode: Click inside the Rich Text element to edit the content, then select the + icon and choose Custom Code to enter the Embed Code mode.

Insert HTML and CSS: In the Embed Code editor, paste the following code block for which you want to add the copy functionality:

<div>
  <p>This is a test paragraph.</p>
  </div>

Note: The code works by impacting the class called “codeToCopy” to identify the div block from which the code is supposed to be copied and it uses the button class “copyButton button” to identify the button which is clicked to copy the code above it.

Step 2: Add JavaScript for Copy to Clipboard

  1. Open Page Settings: Go to the settings of the page where the blog post is located (typically the Blog Posts Template page).
  2. Add Custom Code Before the Closing Body Tag: Scroll down to the "Before </body> tag" section and insert the following JavaScript code:

<script>
  document.getElementById('copyButton1').addEventListener('click', function() {
    const button = this; // 'this' refers to the button that was clicked
    const textToCopy = document.getElementById('codeToCopy1').innerText; // Get the text you want to copy
    
    navigator.clipboard.writeText(textToCopy).then(function() {
      button.innerText = 'Code Copied!'; // Change button text on successful copy
      
      // Optional: Change the text back after a few seconds
      setTimeout(function() {
        button.innerText = 'Copy Code!'; // Revert button text after 3 seconds
      }, 3000);
    }).catch(function(err) {
      console.error('Error in copying text: ', err);
    });
  });
  </script>

Explanation of the Code - The Fun stuff!

  • Explanation of the Code
    • HTML Structure:some text
      • <div class="code-block">: A container for the code block.
      • <pre><code id="codeToCopy">: A pre tag to preserve whitespace and a code tag to indicate code, with an ID for referencing in JavaScript.
      • &lt;div&gt; ... &lt;/div&gt;: The actual code snippet, using HTML entities &lt; and &gt; to display < and > characters. This is necessary to prevent the HTML tags from being rendered.
    • Button:some text
      • <button id="copyButton">Copy Code!</button>: A button to trigger the copy action.
    • CSS Styles:some text
      • <style> ... </style>: CSS to style the div and p elements in the example. This is included to show how styles can be embedded in the code block.
    • JavaScript:
    Explanation of the JavaScript Code:
    1. Query Selectors:
      • Uses document.querySelectorAll('.copyButton') to select all elements with the class copyButton.
    2. Iterate Over Buttons:
      • Iterates over each button using forEach.
    3. Event Listener:
      • Adds a click event listener to each button.
    4. Select Code Block:
      • Uses this.previousElementSibling.querySelector('.codeToCopy') to get the associated code block for the button that was clicked.
    5. Copy Text:
      • Copies the text to the clipboard and provides feedback as before.

Final Implementation

Here's how the final implementation should look in your Webflow project:

Rich Text HTML Embed

<div>
  <p>This is a test paragraph.</p>
  </div>

Custom Code Before </body> Tag

<script>
  document.getElementById('copyButton1').addEventListener('click', function() {
    const button = this; // 'this' refers to the button that was clicked
    const textToCopy = document.getElementById('codeToCopy1').innerText; // Get the text you want to copy
    
    navigator.clipboard.writeText(textToCopy).then(function() {
      button.innerText = 'Code Copied!'; // Change button text on successful copy
      
      // Optional: Change the text back after a few seconds
      setTimeout(function() {
        button.innerText = 'Copy Code!'; // Revert button text after 3 seconds
      }, 3000);
    }).catch(function(err) {
      console.error('Error in copying text: ', err);
    });
  });
  </script>

Conclusion

By following these steps, you can easily add code snippets to your Webflow blog with a convenient copy-to-clipboard functionality. This feature enhances the user experience by allowing your readers to quickly and effortlessly copy code examples for their own use.

For more information and services, visit our Webflow agency. You may also find these related articles useful:

  • Creating Responsive Web Designs in Webflow
  • Optimizing Webflow Sites for SEO

Additionally, you can explore Webflow University for more tutorials and resources.

If you have any questions or need further assistance, feel free to reach out to our team at Paddle Creative. Happy coding!

Ready to Get Started?
Ready to take the next step? Send us a message or give us a call to discuss your project today!