mansi p
mansi p

Reputation: 13

How to make the line break on enterkey in textarea

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.

Upvotes: 1

Views: 4925

Answers (3)

Zirak
Zirak

Reputation: 39808

((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.

Upvotes: 0

Quentin
Quentin

Reputation: 943099

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)

Upvotes: 1

KooiInc
KooiInc

Reputation: 122878

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.

Upvotes: 2

Related Questions