How to Convert Line Breaks to br in jQuery

  Aug 2023
  ITSolutionsGuides
  Category: jQuery
How to Convert Line Breaks to br in jQuery

Hi Developers,

Today we are going to learn about How to Convert Line Breaks to br in jQuery. While using textarea field sometime user will expect their content should be displayed with line breaks as well however the data while storing in the database will be stored avoiding all the line breaks. To avoid this the value in the textarea field should be converted to br for the line breaks given by the user it is done by using the javascript replace() function . lets learn How to Convert Line Breaks to br in jQuery.

Let's Start Coding

Lets see how its done

<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/>

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.

ITSolutionsGuides

ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides