I have a table that has some data that when double clicked to select/copy/paste ends up with some white space on either side of the test (could be from some of the other items inside the cell).
My solution is to have a button show the right of the text that copies the text when clicked.(thanks to jon-p, I have the buttons iterated through the cells)
My question is how can I add a function that copies the text of the cell that the new button is in.
Also, I can't edit the page directly so I'm using tampermonkey to inject the code.
http://jsfiddle.net/pshock13/kcvbyq9r/
<table>
<thead>
<th>Tools</th>
<th>Shipment</th>
<th>Barcode</th>
<th>More Info</th>
</thead>
<tbody>
<tr>
<td><span>✔ ✘</span></td>
<td>
<div class="relative">
<a href="something.com/Search?searchKey=123456789">123456789</a>
</div>
</td>
<td>
<div class="relative">
<a href="na.something.com/results?s=asdfghjkl">asdfghjkl</a>
</div>
</td>
<td>
<div class="relative">
<span>9870356542</span>
</div>
</td>
</tr>
<tr>
<td><span>✔ ✘</span></td>
<td>
<div class="relative">
<a href="something.com/Search?searchKey=987654321">987654321</a>
</div>
</td>
<td>
<div class="relative">
<a href="na.something.com/results?s=qwertyuiop">qwertyuiop</a>
</div>
</td>
<td>
<div class="relative">
<span>asfg456sdfg</span>
</div>
</td>
</tr>
<tr>
<td><span>✔ ✘</span></td>
<td>
<div class="relative">
<a href="something.com/Search?searchKey=123456789">123456789</a>
</div>
</td>
<td>
<div class="relative">
<a href="na.something.com/results?s=asdfghjkl">asdfghjkl</a>
</div>
</td>
<td>
<div class="relative">
<span>9870356542</span>
</div>
</td>
</tr>
<tr>
<td><span>✔ ✘</span></td>
<td>
<div class="relative">
<a href="something.com/Search?searchKey=987654321">987654321</a>
</div>
</td>
<td>
<div class="relative">
<a href="na.something.com/results?s=qwertyuiop">qwertyuiop</a>
</div>
</td>
<td>
<div class="relative">
<span>asfg456sdfg</span>
</div>
</td>
</tr>
</tbody>
</table>
var copyBtn = "<span class='copy' onClick='copyText()'>📋</span>"
var shipmentCells = document.querySelectorAll("tbody tr > td:nth-child(2) > div");
for(var i = 0; i < shipmentCells.length; i++){
//Append the new element to the innerHTML
shipmentCells[i].innerHTML += copyBtn;
}