Gnark
Gnark

Reputation: 4232

How to show newlines from textarea in a table?

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!

Upvotes: 8

Views: 12877

Answers (5)

Geek
Geek

Reputation: 519

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.

Upvotes: 2

vsync
vsync

Reputation: 130810

Set your css in the table cell to

white-space:pre-wrap;

Upvotes: 14

rahul
rahul

Reputation: 187110

\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;
}

Upvotes: 3

AbstractProblemFactory
AbstractProblemFactory

Reputation: 9811

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

Upvotes: 0

Deniz Dogan
Deniz Dogan

Reputation: 26227

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

Upvotes: 2

Related Questions