Copy Text to Clipboard with Line Breaks

PHOTO EMBED

Saved by @mishka #javascript

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

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.