Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery convert line breaks to br (nl2br equivalent)

I'm having jQuery take some textarea content and insert it into an li.

I want it to visually retain the line breaks.

There must be a really simple way to do this...

like image 708
gbhall Avatar asked May 27 '10 07:05

gbhall


1 Answers

demo: http://so.devilmaycode.it/jquery-convert-line-breaks-to-br-nl2br-equivalent

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'); } 
  • http://phpjs.org/functions/nl2br:480
like image 133
Luca Filosofi Avatar answered Nov 02 '22 19:11

Luca Filosofi