Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript line break is not applying when i use document.execCommand("copy")

Tags:

javascript

Hi i'm using below code to build a string and copying it,but in output when i paste it the line break is not applying

function copyToClipboardShipto() {
        var $temp = $("<input>");

        $("body").append($temp);
        var str1 = "@(Model.firstName)"; var str2 = " "; var str3 = "@(Model.lastName)"; var str4 = "\n";
        var str5 = "@(Model.shiptoes[0].address.address1)";
        var str6 = ",";
        var str7 = "@(Model.shiptoes[0].address.address2)";
        var str8 = "\n";
        var str9 = "@(Model.shiptoes[0].address.city)"; var str10 = ","; var str11 = "@(Model.shiptoes[0].address.state)"; var str12 = "\n";
        var str13 = "@(Model.shiptoes[0].address.zip)";
        var str = str1 + str2 + str3 + str4 + str5 + str6 + str7 + str8 + str9 + str10 + str11 + str12 + str13;
        $temp.val(str).select();
        document.execCommand("copy");
        $temp.remove();
    }
}

firstname lastname223 E JACKSON AVE,city,statezip any help appericiated

like image 501
How To Learn Avatar asked Sep 01 '15 05:09

How To Learn


People also ask

What does document execCommand do?

When an HTML document has been switched to designMode , its document object exposes an execCommand method to run commands that manipulate the current editable region, such as form inputs or contentEditable elements.

How do you insert a line break in JavaScript?

The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.

How do you copy to the next line?

“vs code copy line to next line” Code Answer On Windows: Shift + Alt + Up/Down. On Mac: Shift + Option + Up/Down. On Ubuntu: Ctrl + Shift + Alt + Up/Down.


2 Answers

Use <textarea> instead of <input>, since INPUT doesn't support multiline strings.

var $temp = $("<textarea>");
like image 190
davcs86 Avatar answered Sep 26 '22 06:09

davcs86


Use <textarea> instead as <input> wont support line breaks.

like image 39
Chetan Avatar answered Sep 22 '22 06:09

Chetan