Scroll to top
© 2020, Empty Code | All Rights Reserved
Share

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 with 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.


HTML

<input type="text" value="Love to Code" id="textInput">
<button onclick="copyText()">Copy</button>

JavaScript

<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 *