Copy to Clip Board - Widget - Service Portal

Akil
Kilo Expert

Hi All,

I am having the requirement where I need to copy the text filed value  to clipboard when I click on the Copy button. Does anyone come across similar requirement and implemented.

find_real_file.png

HTML Code :

<a href="{{::item.URL}}" target="_blank"> <input id="copyurl" type="text" value={{::item.URL}} class="form-control" readonly></a>
<div class="input-group-btn">
<button ng-click="Copy()" class="btn btn-default" >${Copy}</button>
</div>

Client script :

$scope.Copy = function() {
copyurl.select();
document.execCommand("copy");
}

Thanks,

Akhil

1 ACCEPTED SOLUTION

Akil
Kilo Expert

Below code works.

 

$scope.Copy = function(item) {
var v = document.createElement('textarea');
var copyText=item.URL;
v.innerHTML = copyText;
document.body.appendChild(v);
v.select();
var result = true;
try {
result = document.execCommand('copy');
}
catch(err){
result = false;
}
finally{
document.body.removeChild(v);
}
if(result === true){
spUtil.addInfoMessage("Text copied to clipboard");
}
else{
$window.prompt("${Because of a browser limitation the URL can not be placed directly in the clipboard. Please use Ctrl-C to copy the data and escape to dismiss this dialog}", permalink);
}

};

View solution in original post

7 REPLIES 7

Hi Satish,

There is some problem with 

Getting error : TypeError: Cannot read property 'empty' of undefined
at ChildScope.$scope.Copy

document.selection.empty();

Akil
Kilo Expert

Below code works.

 

$scope.Copy = function(item) {
var v = document.createElement('textarea');
var copyText=item.URL;
v.innerHTML = copyText;
document.body.appendChild(v);
v.select();
var result = true;
try {
result = document.execCommand('copy');
}
catch(err){
result = false;
}
finally{
document.body.removeChild(v);
}
if(result === true){
spUtil.addInfoMessage("Text copied to clipboard");
}
else{
$window.prompt("${Because of a browser limitation the URL can not be placed directly in the clipboard. Please use Ctrl-C to copy the data and escape to dismiss this dialog}", permalink);
}

};

gconway
Tera Contributor

Hi all, I know this is an older post, but is there a way to achieve this on the back-end on a RITM variable which is set as URL type? 

gconway_0-1700742242039.png