I want to be able to say
$(someElem).text('this\n has\n newlines);
and it renders with newlines in the browser. The only workaround I have found is to set the css property 'white-space' to 'pre' on someElem. This almost works, but then I have an annoyingly large padding between the text and the top of someElem, even when I set padding to 0. Is there a way to get rid of this?
<pre>tags (and using .html() instead of .text()) is the easiest and best solution for maintaining line breaks from a text file or from plain text in my opinion (this is suggested by Karim's answer below). HOWEVER: The newer alternative to this is to usewhite-space: pre-wrap;as suggested in cleong's answerappend()instead oftest()and<br/>instead of\n? like this -$(someElem).append("this <br/> has <br/> newlines");