How to convert line breaks to br in jQuery ?

By Hardik Savani | January 9, 2016 | | 481 Viewer | Category : Javascript jQuery


Share this post:

If you are use textarea and you want to like break into br(\n) so that way we can give format as user insert, but if you are working on php then you can easily doing with nl3br() through, but if you are using in jquery or javascript then you have to first declare that. i did give 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/>

Try this..