1

I have a text area. Where On enter it gives me the perfect line break. But When I render it in some templates it is not showing the line break. How can I rectify it? I don't want to use any jquery supported editor for that.

SLaks
  • 837,282
  • 173
  • 1,862
  • 1,933
mansi p
  • 13
  • 1
  • 3
  • If you don't want jQuery, why did you add the `jquery` tag? Also, what on earth do you mean? – SLaks Apr 28 '11 at 14:26
  • How are you rendering it? PHP, for example, has a function to convert newlines into html BR tags. – James Apr 28 '11 at 14:28

3 Answers3

2

welcome to Stackoverflow (SO). Information on subjects like this is easy to find here. Check this SO question, or this one. Next time, try searching SO first.

Community
  • 1
  • 1
KooiInc
  • 112,400
  • 31
  • 139
  • 174
1

The textarea gives you text, but presumably your templates are outputting HTML.

The text for a line break is \n but the HTML for a line break is <br>. You need to replace the former with the latter when converting between data formats.

(You also need to replace characters with special meaning in HTML (<, >, &, ' and ") with their entity counterparts to avoid XSS problems)

Quentin
  • 857,932
  • 118
  • 1,152
  • 1,264
0

((What the hell is a perfect line break?))

I'm guessing that you have a text-area who's text you're saving and then showing it in a div or p or span, and you want the line-breaks to actually show.

There are three ways:

  1. When saving/processing the text, do a search-and-replace for line break characters and replace them with the <br /> element.
  2. Style the element with the white-space:pre; style in CSS.
  3. Wrap it all with the <pre> tag.
Zirak
  • 37,288
  • 12
  • 78
  • 90