Copy from table td to clipboard doesn't work
Besenyi
Can you take a look at this demo and let me know why I can't copy the previous text td
after clicking it .btn-copy
?
$('.btn-copy').on('click', function(){
$(this).closest('td').prev('td').select();
try {
var successful = document.execCommand('copy');
if(successful) {
$('.res').html("Coppied");
}
else
{ $('.res').html("Unable to copy!");}
} catch (err) {
$('.res').html(err);
}
});
table{border:1px solid #ccc;}
td{border:1px solid #ccc;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="res"></div>
<table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
<th>Copy</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td><button class="btn btn-default btn-copy">Copy</button></td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
<td><button class="btn btn-default btn-copy">Copy</button></td>
</tr>
<tr>
<td>Ernst Handel</td>
<td><button class="btn btn-default btn-copy">Copy</button></td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td><button class="btn btn-default btn-copy">Copy</button></td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
<td><button class="btn btn-default btn-copy">Copy</button></td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td><button class="btn btn-default btn-copy">Copy</button></td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
Neil
You can still use document.execCommand('copy');
, but your text selection is incorrect, this should fix it:
$('.btn-copy').on('click', function(){
element = $(this).closest('td').prev('td')[0];
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
//Losely basd on http://stackoverflow.com/a/40734974/7668911
try {
var successful = document.execCommand('copy');
if(successful) {
$('.res').html("Coppied");
}
else
{ $('.res').html("Unable to copy!");}
} catch (err) {
$('.res').html(err);
}
});
table{border:1px solid #ccc;}
td{border:1px solid #ccc;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="res"></div>
<table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
<th>Copy</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td><button class="btn btn-default btn-copy">Copy</button></td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
<td><button class="btn btn-default btn-copy">Copy</button></td>
</tr>
<tr>
<td>Ernst Handel</td>
<td><button class="btn btn-default btn-copy">Copy</button></td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td><button class="btn btn-default btn-copy">Copy</button></td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
<td><button class="btn btn-default btn-copy">Copy</button></td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td><button class="btn btn-default btn-copy">Copy</button></td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>