From df1cac4ee096d7ea0f095349aa9fba8f3b86d242 Mon Sep 17 00:00:00 2001 From: Najm Ajmal <130285427+NajmAjmal@users.noreply.github.com> Date: Sun, 20 Aug 2023 11:16:16 -0700 Subject: [PATCH] Update script.js --- script.js | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/script.js b/script.js index 5db6c0a..b7d4153 100644 --- a/script.js +++ b/script.js @@ -1,28 +1,32 @@ // Get all elements with the "copy-button" class const copyButtons = document.querySelectorAll('.copy-button'); +const copyLinks = document.querySelectorAll('.copy-link'); + +// Function to copy text to the clipboard +function copyTextToClipboard(text) { + const textarea = document.createElement('textarea'); + textarea.value = text; + document.body.appendChild(textarea); + textarea.select(); + document.execCommand('copy'); + document.body.removeChild(textarea); +} // Iterate through each copy button copyButtons.forEach(button => { - // Add a click event listener to each button button.addEventListener('click', () => { - // Get the text to be copied from the "data-copy-text" attribute const textToCopy = button.getAttribute('data-copy-text'); + copyTextToClipboard(textToCopy); + console.log('Text has been copied!'); + }); +}); - // Create a textarea element to hold the text temporarily - const textarea = document.createElement('textarea'); - textarea.value = textToCopy; - - // Append the textarea to the body and select its content - document.body.appendChild(textarea); - textarea.select(); - - // Copy the selected text to the clipboard - document.execCommand('copy'); - - // Remove the textarea from the body - document.body.removeChild(textarea); - - // Alert the user that the text has been copied (you can customize this part) +// Iterate through each copy link +copyLinks.forEach(link => { + link.addEventListener('click', (event) => { + event.preventDefault(); + const textToCopy = link.getAttribute('data-copy-text'); + copyTextToClipboard(textToCopy); console.log('Text has been copied!'); }); });