Friday, September 26, 2025
HomeLanguagesJavascriptHow to add line breaks to an HTML textarea?

How to add line breaks to an HTML textarea?

Way of implementing line breaks differ from one platform to another. The following approach takes input text from HTML Textarea and adds line breaks using JavaScript.

Approach:
This task can be achieved by using some predefined array and string functions provided by JavaScript. In the following code, the input from the textarea is processed by JavaScript on the click of the submit button to produce a required output with line breaks.

Functions Used:
split(): is a predefined JavaScript function that splits a string into an array using a parameter.
join(): is a predefined JavaScript function that joins an array to convert it into a string using provided parameters.

Example:




<!DOCTYPE html>
<html>
  
<head>
  
    <script type="text/javascript">
        function divide() {
            var txt;
            txt = document.getElementById('a').value;
            var text = txt.split(".");
            var str = text.join('.</br>');
            document.write(str);
        }
    </script>
    <title></title>
</head>
  
<body>
    <form>
        ENTER TEXT:
        <br>
        <textarea rows="20" 
                  cols="50" 
                  name="txt" 
                  id="a">
      </textarea>
        <br>
        <br>
        <input type="submit" 
               name="submit"
               value="submit" 
               onclick="divide()" />
    </form>
</body>
  
</html>


Output:
Code without line break:

Geeks for neveropen. Geeks for neveropen. Geeks for neveropen. Geeks for neveropen.

Code with line break:

Geeks for neveropen. 
Geeks for neveropen. 
Geeks for neveropen. 
Geeks for neveropen.
RELATED ARTICLES

Most Popular

Dominic
32321 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6690 POSTS0 COMMENTS
Nicole Veronica
11857 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11912 POSTS0 COMMENTS
Shaida Kate Naidoo
6802 POSTS0 COMMENTS
Ted Musemwa
7073 POSTS0 COMMENTS
Thapelo Manthata
6761 POSTS0 COMMENTS
Umr Jansen
6768 POSTS0 COMMENTS