Scroll to top
© 2022, Empty Code | All Rights Reserved

Copy text to Clipboard using JavaScript


Rahul Kumar Sharma - October 15, 2019 - 0 comments

Instead of Copy text using the shortcut (ctrl+c), JavaScript is the simplest way. We can use the copy command document.execCommand() to copy text content to the clipboard using JavaScript.

Below is the sample code which will show how to copy text to the clipboard using JavaScript.

<input type="text" value="Love to Code" id="textInput">
<button onclick="copyText()">Copy</button>
<script>
function copyText(){
    var text = document.getElementById("textInput");
    text.select();
    document.execCommand("copy");
    alert("Copied text: " + text.value);
}
</script>

Related posts

Post a Comment

Your email address will not be published. Required fields are marked *