A simple JavaScript snippet to copy a string to the clipboard

CK CK 3788 
Created at  
1,053 0 0

The code below is simple and widely used to copy a string to your clipboard.

 

function copyToClipboard(text) {
  navigator.clipboard.writeText(text)
    .then(() => {
      console.log('Text copied to clipboard');
    })
    .catch(err => {
      console.error('Failed to copy: ', err);
    });
}

// Usage
copyToClipboard("Hello World!");
Tags Clipboard Copy To Clipboard Facebook X
Comments 0
Similar posts
  1. How to Get Windows OS Version ?
    429
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    42
  2. Tailwind CSS and shadcn/ui for React App with Charts and Graphs
    1,043
  3. HTML NoIndex: Keep Pages Off Search
    1,495
  4. JavaScript to disable Screen Saver or Power Saving Mode
    501
  5. Improving SEO with image-sitemap
    1,180
  6. Making HTML Tables Responsive on Mobile: Addressing Text Overflow
    774
  7. How do we make better SEO with schema.org?
    424
  8. A Javascript code that returns Monthly Mortgage Payment and Tax
    1,254
  9. Dynamic JSON Editor based in HTML/JavaScript
    511
  10. How to post content (image or video) to Instagram using the Instagram Graph API
    6,395
  11. Font name correctly having the same width space
    1,216
  12. JavaScript Algorithms
    718
  13. GraphQL Tutorial on CentOS 7: From Installation to Deployment
    558
  14. Capture all browser console logs including errors for debugging
    2,174
  15. Simple asynchronous/synchronous $.ajax() example using jQuery to make an HTTP GET request
    850
  16. JavaScript Code to Get Page Keywords
    347
  17. What is OpenGraph?
    367
  18. What is Twitter Cards?
    402
  19. Code to Extract the Site Thumbnail Image for Twitter or Facebook
    393
  20. JavaScript Code to Get the Page Title
    335
  21. JavaScript Code to Get Meta Description from HTML
    396
  22. Javascript code to extract favicon URL
    396
  23. RSS to Array in JavaScript
    2,154
Recently updated
  1. Why SPF and DKIM Are Essential for Linux Email Servers?
    36
  2. How do I create animated background for Google Meet?
    21,011
  3. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    251
  4. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  5. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  6. Array of arguments passed to script in PHP
    90
  7. Invalid Auction Storms in Ad Tech
    98
  8. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  9. The Two Clocks of Ad Server Architecture
    70
  10. Frequency Cap Architecture Overview
    515
  11. Collecting Proof-of-Play from 100,000 Screens
    146
  12. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    288
  13. What is RedisVL?
    109
  14. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    232
  15. TPU & GPU Cluster Management Tools
    245
  16. The Concept of Scaffolding in AI Agents
    234
  17. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  18. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    259
  19. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  20. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  21. Machine Learning: Training vs. Ranking
    222
  22. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  23. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  24. Designing a Data Curation System Around a Vector Database
    221
  25. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  26. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  27. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    196
  28. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    219
  29. Understanding Data Lineage
    262
  30. System Requirements and Architecture: Intelligent Priority-Based Scheduler
    167