I spent a good 20 min searching online for this, but couldn't find it. What I want is to to be able to copy a text string on click without a button. The text string will be inside a "span" class.
- User hovers over text string
- User clicks text string
- Text string is copied to clipboard
Any help would be greatly appreciated. Thanks!
You can attach
copy
event to<span>
element, usedocument.execCommand("copy")
within event handler, setevent.clipboardData
tospan
.textContent
with.setData()
method ofevent.clipboardData
Try this .
document.execCommand('copy')
This is the Code pen.
Jquery Code here
Along with copying the text , you also have to make sure that any previously selected component remains selected after copying to clipboard.
Here's the full code :
HTML:
JS
JS / Function: