jquery - Copy to clipboard multiple items -
i doing copy clipboard picking first id. understand because can't have multiple id's when change code document.getelementsbyclassname
doesn't work?
following code
function copytoclipboard() { var input = document.getelementbyid("toclipboard"); var texttoclipboard = input.value; var success = true; if (window.clipboarddata) { // internet explorer window.clipboarddata.setdata("text", texttoclipboard); } else { // create temporary element execcommand method var forexecelement = createelementforexeccommand(texttoclipboard); /* select contents of element (the execcommand 'copy' method works on selection) */ selectcontent(forexecelement); var supported = true; // universalxpconnect privilege required clipboard access in firefox try { if (window.netscape && netscape.security) { netscape.security.privilegemanager.enableprivilege("universalxpconnect"); } // copy selected content clipboard // works in firefox , in safari before version 5 success = document.execcommand("copy", false, null); } catch (e) { success = false; } // remove temporary element document.body.removechild(forexecelement); } if (success) { alert("the text on clipboard"); } else { alert("your browser doesn't allow clipboard access!"); } } function createelementforexeccommand(texttoclipboard) { var forexecelement = document.createelement("div"); // place outside visible area forexecelement.style.position = "absolute"; forexecelement.style.left = "-10000px"; forexecelement.style.top = "-10000px"; // write necessary text element , append document forexecelement.textcontent = texttoclipboard; document.body.appendchild(forexecelement); // contenteditable mode necessary execcommand method in firefox forexecelement.contenteditable = true; return forexecelement; } function selectcontent(element) { // first create range var rangetoselect = document.createrange(); rangetoselect.selectnodecontents(element); // select contents var selection = window.getselection(); selection.removeallranges(); selection.addrange(rangetoselect); }
and here html;
<div id="app-details"><span>activation guid</span><br /><br /> <input id="toclipboard" value="@appdetails.activationguid"> <button onclick='copytoclipboard ()'>copy guid</button></div>
thanks.
Comments
Post a Comment