I have a table with some of its columns containing text that is gonna be copied quite often. Despite knowing no javascript, I adventured myself into the challenge of making the content copyable on click and managed to get this working:
function copy(event) {
console.log("Triggered")
var target = event.target || event.srcElement;
console.log("Targeted")
var copyText = target.innerText || target.textContent;
navigator.clipboard.writeText(copyText)
}
var plist = document.querySelectorAll("p");
for (var i=0; i < plist.length; i++) {
plist[i].addEventListener("click", copy)
}
<!DOCTYPE html>
<html>
<body>
<h1>Copy selector Test</h1>
<p>Hi There.</p>
<p>I want to copy</p>
<p>each of these p's</p>
<p>by clicking them.</p>
</body>
</html>
Well, considering my javascript knowledge, I'm pretty sure that isn't the most efficient or correct way of doing it (looping each element and adding an eventlistener to each one seems a bit of unnecesary work to me). Any correction is welcome.
Now let's say I want each of the cells of the "Last" column to be copied in the same way that the p's were copied in my snippet. How could I get it done? It would be also cool to highlight the text that's been copied (I tried with .select() but apparently that only works with input elements). The table is gonna be populated dynamically with django, by the way.
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
Thanks a lot guys!
JP.
UPDATE: In case this is useful to someone, I've managed to make a column copyable on click by adding a "copyable" class to its . This is the code:
function indexInParent(node) {
var children = node.parentNode.childNodes;
var num = 0;
for (var i=0; i<children.length; i++) {
if (children[i]==node) return num+1;
if (children[i].nodeType==1) num++;
}
return -1;
}
var columns = document.getElementsByClassName("copyable")
for (var i=0; i < columns.length; i++) {
var column = columns[i];
var index = indexInParent(column);
document.querySelectorAll("td:nth-child("+index+")")
.forEach(elem => elem.addEventListener("click", copy))
}