See answer below.
Also see: How do I copy to the clipboard in JavaScript? for an older approach.
Original question:
I want to be able to copy a table-cells value whenever a user clicks.
I've tried this:
function copyToClipboard(text) {
var selectTableCells = document.querySelector('td');
selectTableCells.addEventListener('click', function(event) {
console.log("You copied: ", selectTableCells);
copyToClipboard(selectTableCells.innerHTML);
});
}
td,
th {
border: 1px solid #ccc;
display: block;
background-color: #ccc;
width: 160px;
}
td {
cursor: pointer;
text-align: center;
}
<table id="table" class="responsive" style="width:1000px;">
<tbody>
<thead>
<tr>
<th>Field Type</th>
</tr>
</thead>
<tbody>
<tr>
<td id="cell1">Click me to copy!</td>
</tr>
</tbody>
</table>
</div>
<input type="text" style="height:50px;width:300px;" placeholder="For proof of concept. Try to paste here">