Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript | save textarea value with line breaks

Tags:

People also ask

How do you add 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 insert a line break in input field?

Basic HTML Line Break Syntax You can insert line breaks in HTML with the <br> tag, which is equivalent to a carriage return on a keyboard. Be aware that HTML will ignore any line break from a keyboard's return key.

How do you start a new line in text area?

\n is the linefeed character literal (ASCII 10) in a Javascript string. <br/> is a line break in HTML.


I have a code where it saves multiple textarea values in a text file. However, it does not display the line breaks I indicated after saving it. It only identifies the line breaks w/c are manually put within the textarea. Below is the code. Please help.

<script>
    var TestVar = new Array(); 
    var i = 0;
    function save()
    {
        TestVar[i] = document.getElementById("text1").value + "\n" + document.getElementById("text2").value;
        mydoc = document.open();
        mydoc.write(TestVar);
        mydoc.execCommand("saveAs",true,"TicketID.txt");
        mydoc.close();
    }
</script>
</head>
<body>
    <form id=formtest>
        <textarea name="textarea" id="text1"></textarea>
        <textarea name="textarea" id="text2"></textarea>
        <input type="button" value="save" onclick="save()">
    </form>
</body>