views:

112

answers:

4

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.

+1  A: 

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.

Lee
Thank you Lee, it worked correctly
tlpd
A: 

Give <br> without the ending slash.

Mamta Dalal
If he's using XHTML DOCTYPE `<br>` won't validate.
Robert
A: 

Since you're retrieving it from the DB, why do it client-side and not do the replace as soon as you retrieve the data from your DB ? Not sure what you're using (although that's irrelevant since this can be done in any language) but in PHP, assuming $output is your DB result it would be as easy as

$output = nl2br($output);

Hope this helps !

FreekOne
PHP has the `nl2br()` function for this reason.
Robert
@FreekOne: is he using php? O.o
David Thomas
I was addressing FreekOne who gave a solution *if* the language is PHP.
Robert
@Robert, me too :) (edited to make that clear.)
David Thomas
+1 Agreed Robert, I have updated my answer. Completely forgot about that :)
FreekOne
@David: I don't know either, I was only assuming for the proof of concept.
FreekOne
Thank FreekOne and all of you. As i am using Java so the above function could not work. I tried to use dto.accPassage = dto.accPassage.replace(System.getProperty("line.separator"), "< br/>"); inside Java code to convert and it worked correctly. However, in some case i have to format data inside a grid, so if i use java code i have to loop one time before displaying at JSP.
tlpd
@tlpd: In that case I'm sorry but I can't help you further as I do not know Java, but maybe it would be a good idea to run a query on the DB -- if possible, of course -- to edit the stuff that's already there, and then modify the form parser so the inserted data would already contain `<br>`s in the future, eliminating the issue alltogether.
FreekOne
@FreekOne: I appreciated your help. As i tried Lee'way and it worked as what i wanted. Thank you very much.
tlpd
A: 

Since you're using java, you could always write something like the following to replace the new lines after you've retrieved the data from database.

public static String nl2br(String s) {
    return s.replaceAll("\n","<br/>");
}
// usage
nl2br("some\ntext"); //will return "some<br/>text"
mmhan
Thank mmhan, as i tried your way but it did not work. however, if i use return s.replaceAll(System.getProperty("line.separator"),"<br/>");, it will work like a charm
tlpd