JavaScript is a programming language that adds interactivity and dynamic behavior to web pages.
On this page, JavaScript is used to retrieve the text from a textarea when the user clicks the copy button, and then copy that text to the system clipboard.
The feature used in this example is the
Clipboard API,
specifically
navigator.clipboard.writeText().
It is the recommended modern method for copying text and is supported by most current web browsers.
function copyText(){
const text = document.getElementById("text").value;
navigator.clipboard.writeText(text);
}
This creates a function named copyText(). The function runs whenever the user clicks the copy button.
This retrieves the current text entered in the textarea whose ID is text.
This copies the retrieved text to the system clipboard.
copyText() function is executed.navigator.clipboard.writeText() is the recommended modern way to copy text.