I would suggest working with someElem directly, since replacements with .html() also replace other HTML tags inside the string.
Here is my function:
function nl2br(el) { var lines = $(el).text().split(/\n/); $(el).empty(); for (var i = 0 ; i < lines.length ; i++) { if (i > 0) $(el).append('<br>'); $(el).append(document.createTextNode(lines[i])); } return el; }
Call by:
someElem = nl2br(someElem);
JochenJung 02 Oct '13 at 10:38
source share