admin管理员组文章数量:1427333
I am binding the data to html table by using angularjs
functionality in separate .js
file. Where as I want to copy these bounded text data and paste it to any text document by clicking the input button below.
<button type="button" class="btn" ng-click="selectElementContents();"></button>
and in angular controller written function as below..
$scope.selectElementContents = function () {
copyTblData();
}
function copyTblData() {
var copyText = document.getElementById('tablerecords');
$('#tablerecords').focus();
$('#tablerecords').select();
document.execCommand('copy');
}
where I am doing mistake not understandable, required suggestion is appreciated.
I am binding the data to html table by using angularjs
functionality in separate .js
file. Where as I want to copy these bounded text data and paste it to any text document by clicking the input button below.
<button type="button" class="btn" ng-click="selectElementContents();"></button>
and in angular controller written function as below..
$scope.selectElementContents = function () {
copyTblData();
}
function copyTblData() {
var copyText = document.getElementById('tablerecords');
$('#tablerecords').focus();
$('#tablerecords').select();
document.execCommand('copy');
}
where I am doing mistake not understandable, required suggestion is appreciated.
Share Improve this question asked Oct 23, 2018 at 18:31 abbasabbas 531 silver badge3 bronze badges 2- Is your table selected? – Poul Bak Commented Oct 23, 2018 at 18:54
- no, its not getting selecting even. – abbas Commented Oct 23, 2018 at 18:56
1 Answer
Reset to default 4For select data , you must use ranges and select. You can try it:
let table = document.querySelector('#testTable');
let button = document.querySelector('#button');
function selectNode(node){
let range = document.createRange();
range.selectNodeContents(node)
let select = window.getSelection()
select.removeAllRanges()
select.addRange(range)
}
button.addEventListener('click',function(){
selectNode(table);
document.execCommand('copy')
})
td{
border:1px solid black;
}
<table collapsed id = 'testTable'>
<tr>
<td>test</td>
<td>test</td>
</tr><tr>
<td>test</td>
<td>test</td>
</tr>
</table>
<br/>
<button id = "button">select</button>
本文标签: javascriptHow to copy the text of html table to clipboardStack Overflow
版权声明:本文标题:javascript - How to copy the text of html table to clipboard? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745495296a2660779.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论