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:

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

function insertLines()
{

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

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

}

</script>
</head>
<body>

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

</body>
</html>


Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com