How to convert line breaks to br in jQuery ?

  34 views   6 days ago jQuery

If you are using the textarea in your application and users input some paragraph in the textarea and application store this input in a database and you fetch this info and display on a web page then you many time face the input data not display like a paragraph. but if you are working on PHP then you can easily be doing with nl3br() through, but if you are using in jquery or javascript then you have to first declare that. I did give a full example of jquery Textarea line breaks asunder.

<body>
    <textarea class="textareac"></textarea>  
    <script>  
      
    var text_content = <?php echo $content ?>;  
      
    function nl2br (str, is_xhtml) {     
        var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br/>' : '<br>';      
        return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');  
    }  
      
    $(".textareac").val(nl2br(text_content));  
      
    </script>  
<body/>

i hope you like this solution.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com

Related Articles