8

In a form I have a textarea where obviously text is entered. When the input is finished the content gets submitted to the server and is being stored in a database...

When I display the input the user made within a table, the newlines are not visible. When I inspect the source the newlines are there, but within a table the newlines do not work...

Is there any possibility of displaying the linebreaks within that table? I know, probably a really silly question but I'm not to pro when it comes to things like html and css...

Any help is really appreciated!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Gnark
  • 4,080
  • 7
  • 33
  • 44

5 Answers5

14

Set your css in the table cell to

white-space:pre-wrap;
vsync
  • 118,978
  • 58
  • 307
  • 400
3

\n won't be rendered as a new line in HTML. You have to use a <br/> to achieve this effect.

Use a string replace to replace all '\n' characters to '<br/>'

If you are using a server side language like C# you can do this

private string PutLineBreaks(string strData)
{
    string strReplaced = string.Empty;

    Regex r = new Regex("/\n/g");

    strReplaced = r.Replace(strData, "<br/>");

    return strReplaced;
}
casperOne
  • 73,706
  • 19
  • 184
  • 253
rahul
  • 184,426
  • 49
  • 232
  • 263
  • actually, Id rather use a little JavaScript snippet to accomplish that task! anyway, thanks for the info! – Gnark Sep 24 '09 at 09:29
  • 1
    The `\n` is rendered as a newline within `
    ` tags, or in any block with various settings (e.g. 'pre', 'pre-line') of the `white-space` CSS setting, see e.g. http://www.w3schools.com/cssref/pr_text_white-space.asp
    – Brian M. Hunt Oct 14 '11 at 00:49
2

You want to replace all \n with <br/>.

Deniz Dogan
  • 25,711
  • 35
  • 110
  • 162
2

Set your css in the table cell to

white-space:pre-line;

Even if your data has trailing spaces it would remove and make data perfect.

Geek
  • 481
  • 5
  • 20
0

Use <br /> , or rather <p></p> if we talked about some text and new paragraph. (semantic better solution).

IProblemFactory
  • 9,551
  • 8
  • 50
  • 66
  • I'm not sure replacing \n with a

    would be semantically correct actually.
    is semantically a line break, as is \n. \n\n however would semantically be a paragraph.
    – Deniz Dogan Sep 24 '09 at 09:28
  • 1
    You'd want to start the entire text block with a '

    ', and close the entire text block with a '

    '. Then you can replace each '\n' with a '

    ', which will close one paragraph and start another.

    – Jarrett Meyer Sep 24 '09 at 09:36