'Display text with linebreaks when output from database

So i have set up a news page on my website, the user can add posts to the website through a text area in a dashboard. The problem i am having is when the news post is added it does not displaying any line breaks.

So for example it should read like -

"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip."

But instead it reads

"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip"

The link breaks in my database(mongodb) are stored as \r\n' + but i cant get it to display with the line breaks.

I am using EJS to dispay it in my page.



Solution 1:[1]

When you display text in an HTML element like <p>, line breaks are treated like spaces and the <p> element finds its own line breaks. To achieve what you want, you can split the text that comes from database at the line breaks:

var listOfParagraphs = textFromDatabase.split("\r\n");

and generate one <p> element per entry in the listOfParagraphs array.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Heiko Theißen