3 works

Copy Text to Clipboard with Line Breaks


dashboard+ Project

Posted by @mishka #javascript

Code snippet:

function copyToClipboard(){

    var codeToBeCopied = document.getElementById('code-snippet').innerText;
    var emptyArea = document.createElement('TEXTAREA');
    emptyArea.innerHTML = codeToBeCopied;
    const parentElement = document.getElementById('post-title');
    parentElement.appendChild(emptyArea);

    emptyArea.select();
    document.execCommand('copy');

    parentElement.removeChild(emptyArea);
    M.toast({html: 'Code copied to clipboard'})

    }
content_copyCopy to Clipboard

More details:

Code used in the Copy to Clipboard button on this site. The final line is a toast to display a message in Materialize that the text has been copied. You can skip it.


>> Browse more code snippets

more_vert