-1

In my code, I am trying to copy iframe code using copy to clipboard button but I am not satisfied with it because when I copy using button it copies &lt; and &gt; instead of < and > secondly it won't highlights the text area so is there any alternate solution to copy as HTML code thanks Here is my JSfiddle

Here is a sample copied text

&lt;iframe src='http://localhost/secvideo/cms/watch?v=30Rt9r' frameborder='0' style='overflow: hidden; position: absolute;' height='100%' width='100%'&gt;&lt;/iframe&gt;    

and Here is my JS

function copyToClipboard(elementId) {
var aux = document.createElement("input");

// Assign it the value of the specified element
aux.setAttribute("value", document.getElementById(elementId).innerHTML);
document.body.appendChild(aux);
aux.select();
document.execCommand("copy");
document.body.removeChild(aux);
alert("Copied!");
}
Rtra
  • 504
  • 10
  • 22

2 Answers2

1

I think you are creating an element unnecessarily here. You already have a text area with the iframe content.

All you need is to select the text area and do a document.execCommand("copy");

Modify your script to

window.copyToClipboard = function(elementId) {

  // Create a "hidden" input
  var aux = document.getElementById(elementId);
  // Highlight its content
  aux.select();
  // Copy the highlighted text
  document.execCommand("copy");
  alert("Copied!");
}

jsfiddle https://jsfiddle.net/yhpe990k/1/

karthick
  • 11,101
  • 5
  • 48
  • 79
1
    var copyToClipboard = function (text) {
        var $txt = $('<textarea />');

        $txt.val(text)
            .css({ width: "1px", height: "1px" })
            .appendTo('body');

        $txt.select();

        if (document.execCommand('copy')) {
            $txt.remove();
        }
    };
Alper Ebicoglu
  • 6,488
  • 1
  • 34
  • 39