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,457
  • 1
  • 13
  • 26
Abhimanyu
  • 4,752
  • 7
  • 33
  • 44

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ó
  • 807,428
  • 183
  • 922
  • 838
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
  • 51,583
  • 38
  • 133
  • 185
  • 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