Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display line break in <div> tag from database

I have to display an article from database inside a < div> tag. This article was inserted into database from a textarea. My problem is: i could not display exactly the structure that i inserted from the textarea (including line break)

I tried the below code to replace the enter character to < br> tag but it did not work

<div id="tmpId">${f:h(dto.accPassage)}</div>

<script>
    $(function(){
        $('#tmpId').html($('#tmpId').html().replace(/\n/g, '<br />'));
    })
</script>

I wonder if someone could give me some hints to solve this problem.

Thank you very much.

like image 240
Phu Nguyen Avatar asked Dec 13 '22 19:12

Phu Nguyen


1 Answers

if you want it to be exactly as it is in the database, then just render it inside a <pre> tag, instead of a <div>.

<pre id="tmpId">${f:h(dto.accPassage)}</pre>

That will preserve the exact formatting in the enclosed text block.

like image 159
Lee Avatar answered Dec 26 '22 11:12

Lee