How to convert line breaks to br in jQuery ?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this blog I will show you how to convert line breaks to br in jQuery. When we are use textarea and if we want to like break a line into br(\n) so on that way we can give the format as well user insert, but if we are working on php then we can easily doing it with nl3br() through, but if we are using in jquery or javascript then we need to first declare that. Here i will give a full example of jquery textarea line breaks as under.

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

Read Also : Laravel 8 Full Calendar Tutorial

Thanks for read this, I hope it will you. You can also follow us onĀ Facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →