JavaScript Answers

How to copy a URL to the clipboard with JavaScript?

Spread the love

To copy a URL to the clipboard with JavaScript, we can call navigator.clipboard.writeText with the URL string.

For instance, we write:


to add a button.

Then we write:

const button = document.querySelector('button')
button.onclick = () => {

We get the button with document.querySelector.

Then we set the button.onclick property to a function that calls navigator.clipboard.writeText with '' to copy '' to the clipboard.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.