-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
39b797c
commit df1cac4
Showing
1 changed file
with
21 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!'); | ||
}); | ||
}); |