Add a "click to copy" button with JavaScript

Would you like to make it quick and easy for your users to copy text onto your project? That famous link to share your article? That affiliate link that helps you generate extra sales? Then this article is for you! Let's take a look at the two main methods for adding a "click to copy" button with the help of JavaScript.
•
Updated on August 14, 2026
•
3 min read
Believemy logo

Would you like to allow your users to quickly and easily copy text on your project? That famous link to share your article? That affiliate link that helps you generate additional sales? Then this article is for you! Let's explore together the two main methods that will allow you to add a "click to copy" button with the help of JavaScript.

 

Let's Start with an Example

To understand how we'll do this, we suggest starting with a small example.

This is a textarea that contains default text, but you can also use an input. We will also add a button to show our users that they can quickly copy the content.

HTML
<div class="wrapper">
  <textarea rows="10">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</textarea>
  <button>Copy</button>
</div>
CSS
textarea {
  width: 100%;
  padding: 20px;
  font-size: 15px;
  outline: none;
  border: 1px solid #613bdb;
  border-radius: 10px;
  background: #c1c9df;
}

button {
  height: 45px;
  width: 155px;
  color: #fff;
  background: #613bdb;
  border: none;
  border-radius: 5px;
  font-weight: 400;
  margin-top: 8px;
  cursor: pointer;
}

Note that we are not using classes (.class) or IDs (#id), because we only have two elements.

 

First Objective: Detect the Click

In order to copy our text, we first need to detect when our user clicks on the "Copy" button.

To do this, we start by selecting our textarea (or input) with document.querySelector and do the same for the button.

JAVASCRIPT
let editor = document.querySelector("textarea");
let button = document.querySelector("button");

Now that our elements are targeted, we just need to add an event listener to the button to detect the click.

JAVASCRIPT
button.addEventListener('click', () => {
  console.log('I am clicked');
});

Great! We can now copy the content of the textarea (or input) when the button is clicked.

 

Second Objective: Copy the Text

To successfully copy the text, we have two methods that we can use. The first involves using the document object and the second uses navigator.

Let's start with the document object.

 

Method A

What we want to do is both:

  1. Select the text
  2. Copy the text
  3. Modify the button text (to display a nice confirmation message, for example)

Here's what we'll do with JavaScript.

JAVASCRIPT
button.addEventListener('click', () => {
  editor.select();
  button.innerText = "Copied!";
});

With select() and innerText, we can select the text content and modify the button's content. However, we still can't copy the text. Here's how to do it.

JAVASCRIPT
button.addEventListener('click', () => {
  editor.select();
  document.execCommand("copy");
  button.innerText = "Copied!";
});

With execCommand(), we can use any command (copy, paste, cut, etc.). Here, we want to copy.

 

Method B

Let's move on to the second method. This one takes the code we previously used to select the content and modify the button text but changes the way we copy the text.

JAVASCRIPT
button.addEventListener('click', () => {
  editor.select();
  // document.execCommand("copy"); // method number 1
  navigator.clipboard.writeText(editor.value); // method number 2
  button.innerText = "Copied!";
});

With the navigator object, we can access the clipboard and write text to it. We use editor.value to retrieve the content of the textarea (or input).

 

It's as simple as that! You now know how to copy text very quickly and easily. If you want to learn more about HTML and CSS, we have a comprehensive course dedicated to these two programming languages. Also, feel free to share this article with your network on your social media. Thank you!

Category: Developers

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.

Believemy logo
Comments (0)

You need to be logged in to comment on this article: log in or sign up.

Read next

Create a share button pasted at the bottom of the page with CSS

Displaying a social networking sharing button on your site helps to increase the number of visits. Nowadays, it has become essential to offer this kind of button whenever we publish articles or project presentations. Let's find out how to set up this kind of button in this article!

December 9, 2024

Create a social network sharing button

For your blog posts or on the pages that present your projects from a portfolio, presenting a modern, non-intrusive button to allow your visitors to share your website or go to your social networks is ideal. Let's look at how to create this type of button together in this article.

December 9, 2024

Syntax highlighting with HTML and CSS

If you have a blog or a portfolio on which you want to present code, whether HTML / JavaScript or CSS code extracts, you have undoubtedly come up against a problem: how do you activate syntax highlighting to make it look as if you had put the code in a code editor? In this article, we'll look at how to do it.

December 9, 2024

Animate emojis like on Discord with JavaScript

Would you like to create a button whose emoji changes every time you move your mouse over it? Like on Discord? Then this article is for you!

December 9, 2024

Create a heartbeat button

For a main button or even for a button you want to highlight, offering this heartbeat effect can give your website a very modern feel. Find out how in this article.

December 6, 2024

Creating circled buttons on hover with HTML and CSS

For a menu or to encourage your readers to share your website, share buttons are essential elements to add to your website. Let's find out how to make them more modern with a "neon" effect that will surround the icon selected by your user.

December 9, 2024

Make an arrow that expands when hovered over with CSS

Whether it's to make your users want to see your project, read your article or access a page on your website, presenting your link with an arrow will make it easier to catch your visitors' eye! Let's find out in this article how to animate this arrow to enhance your readers' experience.

December 9, 2024

Creating a NEON button with CSS

Creating a button is one thing! But creating a button that your visitors want to click is something else entirely. In any case, this neon button will catch the eye of everyone who visits your pages.

December 9, 2024

Become the solopreneur you dream of being.

First project, career change, business launch: access our courses, practice on real-world cases, and move forward with clear guidance.