how to copy text from a div to clipboard

Solutions on MaxInterview for how to copy text from a div to clipboard by the best coders in the world

showing results for - "how to copy text from a div to clipboard"
Alois
16 Apr 2018
1function (container_id, as_html) {
2        /*
3        isEmpty = function (val) {
4              return (val === undefined || val == null || val.length <= 0) ? true : false;
5          };
6        */
7        as_html = isEmpty(as_html) ? false : true;
8
9        //console.log('copyToClipboard: ' + container_id);
10        var elem = document.getElementById(container_id);
11
12        // create hidden text element, if it doesn't already exist
13        var targetId = "_hiddenCopyText_";
14        var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
15        var origSelectionStart, origSelectionEnd;
16        if (isInput) {
17            // can just use the original source element for the selection and copy
18            target = elem;
19            origSelectionStart = elem.selectionStart;
20            origSelectionEnd = elem.selectionEnd;
21            //console.log ('_is_input = true');
22        } else {
23            // must use a temporary form element for the selection and copy
24            //console.log ('_is_input = false, creating a textarea = ' + targetId);
25            target = document.getElementById(targetId);
26            if (!target) {
27                var target = document.createElement("textarea");
28                target.style.position = "absolute";
29                target.style.left = "-9999px";
30                target.style.top = "0";
31                target.id = targetId;
32                document.body.appendChild(target);
33            }
34
35            var content = $.trim(as_html ? $(elem).html() :  $(elem).text());
36            $(target).text( content);
37            console.log ('text_content', content);
38        }
39        // select the content
40        var currentFocus = document.activeElement;
41        target.focus();
42        target.setSelectionRange(0, content.length);
43
44        // copy the selection
45        var succeed;
46        try {
47            succeed = document.execCommand("copy");
48        } catch (e) {
49            succeed = false;
50        }
51        // restore original focus
52        if (currentFocus && typeof currentFocus.focus === "function") {
53            currentFocus.focus();
54        }
55
56        if (isInput) {
57            // restore prior selection
58            elem.setSelectionRange(origSelectionStart, origSelectionEnd);
59        } else {
60            // clear temporary content
61            $(target).text('');
62        }
63
64        return succeed;
65    }
queries leading to this page
example of copy to clipboard with copy to clipboardcss copy text to clipboardcopy button text on click to other devicecopy to buffer text from div by click javascriptcopy to clipboard everything inside a div htmlhow to make copy to clipboardcopy to clipboardcopy url to clipboard htmlcopy div text to clipboard typescripthtml copy textcontent of element to clipboarddiv copy to clipboardcopy to clipboard with a linkcopy content of div incl element to clipboardhow to copy text from a div to clipboardcopy link to clipboardcopy to clipboard divcopy contents of div with format javascriptcopy content of div to clipboardjquery copying text from divcopy a whole div to clipboardjavascript copy div to clipboardcopy div to clipboard javascripton add bosttone copy the fiv injquery copying text in divcopy to clipboard linkclick to copy to clipboardcopy div text to clipboardjavascript copy to clipboard from divjquery compy content on clickcopy all text of div to clipboardhow to copy textbox text to clipboardcopy div text to clipboard jqueryjavascript copy text to clipboard on paragraphcopy text to clipboard from divwhen click div copycopy text from a divhtml copy text to clipboard without stylehow to set a copy button to copy the conetnts in a div htmljs click to copy text in divhow to copy text in a tag to clipboardcopy texto from paragraph clipboard javascriptcopy text to clipboardcopy text from div htmljavascript click to copy text in divhtml button to copy text in a divhow to copy text from div in javascriptcopy to clipboard a hrefjavascript copy content from div javascript copy content of div to clipboardcopy a link to clipboardcopy element content to clipboardjavascript copy text from div to clipboardjavascript code to copy a linkjavascript copy div content to clipboardadd div content to clipboardclick to copy text of a divcopy paragraph text in javascriptcopy element text to clipboardcopy text from div to clipboardcopy to text from div buffer by click javascripthow to copy a paragraph in javascriptcustom copy to clipboard copy text content to clipboardcopy to clipboard anchorhow to copy text of an element in clipboard javascriptjquery copy to clipboard divjquery copy div content to clipboardweb clipboard copy pastecopy div content to clipboardcopy to clipboard div contentmake something copy to clipboard when clickhow to copy text from a div to clipboard