0

I have some content like this

var p =  

 I myself
 Abhimanyu Singh
 Yadav

when I m trying to insert into as innerHTML to some div the whole content appears in one line. I'm using <pre> tag to avoid this problem. But need some appropriate solution.

Alex
  • 1,197
  • 1
  • 10
  • 22
Abhimanyu
  • 4,652
  • 7
  • 32
  • 43

2 Answers2

5

You can replace the new line characters with html BR elements (<br />):

document.getElementById('myDiv').innerHTML = p.replace(/\n/g, '<br />');
Christian C. Salvadó
  • 769,263
  • 179
  • 909
  • 832
1
var p = "i myself<br />Abhimanyu Singh<br />Yadav";

updated to make andrew happy. a still non-semantic answer that is more semantic.

Jason
  • 49,989
  • 37
  • 128
  • 183
  • Totally non-semantic. A terrible idea. It will style as a paragraph break instead of a line-break. – Andrew Moore Aug 10 '09 at 05:58
  • yes, it appears this OP is worried about semantics, first off, as he is trying to insert line breaks in completely random places. when a user asks something ridiculous like this, i give an answer that works. if he wants to replace the `

    ` tags w/`
    ` tags, whatever

    – Jason Aug 10 '09 at 06:23