I trying to copy data to clipboard , I want it so that whenever a user clicks on a span that contain text, that specific text it is matched up with is copied to the clipboard.
<tbody data-bind="foreach: closedAccounts">
<span id="a" data-bind="text: $data.accountNo" onclick="copyDivToClipboard()"
data-toggle="tooltip" title="Copy to clipboard">
</span></tbody>
javascript function
function copyDivToClipboard() {
var range = document.createRange();
range.selectNode(document.getElementById("a"));
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
document.execCommand("copy");
console.log(range);
}
I am having trouble with copying that specific text that matches a span since the text does not have a specific id or class name because it is just printing text as it goes through the loop, so they all have the same id. So how would I specify a specific text with that span, since the spans could get pressed in any order?
You need to use the
thiskeyword. Passthisinto your onclick function and you will be able to access the clicked element and all of its properties inside the function.