Search

How to convert line breaks to br in jQuery ?

post-title

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.