Sep 1, 2010

Display Text Area Contents with Line Breaks as Html Content

There was a need for me to display the text area contents as html in one of my assignments. I was able to see that the line breaks (\n) present in the text content that needs to be displayed as html in a page were getting stripped of automatically!

Was wondering why the text area content with line breaks was not getting displayed properly as html on the first attempt. Then found that the \n were auto stripped by html while displaying the text field contents with line breaks as html.

So, here is the fix that I incorporated in the html page that was displaying the text area content with line breaks. The best way via which I was able to fix this up was as below:

Let us say content is the field that has the text area content stored in it from the database.
While displaying that in the html page, I used
content.replaceAll(’\n’,’<<br />>’) 

And this worked!