copy text to clipboard with break lines
If we need to copy text to clipboard with break lines, than we need to use
textarea
instead of using
input
element
function copy(part1, part2) {

    // Creates textarea element 
    var textarea = document.createElement('textarea');

    // Puts the text into the textarea with breaklines
    textarea.value = part1 + "\n\n" + part2

    // Adds the element to the DOM
    document.body.appendChild(textarea);

    // Selects the text that we want to copy
    textarea.select();

    // Copies the selected text clipboard
    var result = document.execCommand('copy');

    // Removes the element from the DOM because we no longer need it
    document.body.removeChild(textarea);
    return result;
}
by Valeri Tandilashvili
4 years ago
JavaScript
3
Pro tip: use ```triple backticks around text``` to write in code fences