CancelImage Upload

How to insert line breaks into a Textarea with JavaScript

Inserting line breaks into a textarea with JavaScript is not as bigger operation as it may sound. In essence a line break inside a textarea is defined as \n. There is no need to insert BR tags or the like.

Here is an example of a function to include some line breaks with text dynamically. On clicking the button below the textbox the function insertLines() is set in motion to insert the new lines:

<title>Insert new lines dynamically</title>
<script type = "text/javascript">

function insertLines()

       var textArea = document.getElementById("textarea");

       textArea.value = textArea.value + "\nLine1\nLine2\nLine3";



<textarea style="width:450px; height:150px;" id="textarea">Click on the button below to add some lines!</textarea>
<input type="button" onclick="javascript:insertLines()" value="Insert">


Want to leave a comment?

No problem. Just enter your email and password below.

register | home | reminder

myDesignTool Networking •