Dialog content to clipboard
Hello,
the below page simply open a jqueryUI-dialog box printing same colored text.
Ideally the "Copy to clipboard" button should make a copy available to user.
Unfortunately leveraging on textarea element and document.execCommand('copy') it only copy the underlying code:
- ie: <span style='color:red;'>This is a red text</span><br><span style='color:green;'>This is a green text</span><br><span style='color:blue;'>This is a blue text</span><br>
If instead I press CMD+A, CMD+C then paste with CMD+V I get colored text plus additional unwanted information since CMD+A has selected the entire page.
Does anybody has suggestion on how to color-copy to clipboard only the UI Dialog content ?
Thx!
- <!DOCTYPE html>
- <html>
- <head>
- </head>
- <body>
- <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.js"></script>
- <script type="text/javascript" src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
- <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
- <script type="text/javascript">
- function clip(text) {
- var copyElement = document.createElement('textarea');
- copyElement.setAttribute("id", "mytextarea");
- copyElement = document.body.appendChild(copyElement);
- $("#mytextarea").val(text);
- copyElement.select();
- try {
- if(!document.execCommand('copy')) throw 'Not allowed.';
- } catch(e) {
- copyElement.remove();
- console.log("document.execCommand('copy'); is not supported");
- prompt('Copy the text below. (ctrl c, enter)', text);
- } finally {
- if (typeof e == 'undefined') {
- copyElement.remove();
- }
- }
- }
- function to_clip(){
- clip($txt);
- }
- $(function(){
- $txt="";
- $txt+="<span style='color:red;'>This is a red text</span><br>";
- $txt+="<span style='color:green;'>This is a green text</span><br>";
- $txt+="<span style='color:blue;'>This is a blue text</span><br>";
- $txt2="<br><input onClick='to_clip();' id='submitbtn' type='submit' value='Copy to clipboard'>";
- var title="Clip-test";
- var mypopup2 = $("<div id='dialog' title='"+title+"'></div>", { css: { 'display': 'none' }});
- mypopup2.html($txt+$txt2);
- mypopup2.dialog({ width: 400, height: 200 });
- $("#dialog").css({"font-size": "11px"});
- })
- </script>
- Test
- </body>
- </html>