How to convert line breaks to br in jQuery ?

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


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


Do you want to expert php developer or developer team for your projects ?
Contact US