Let's say I have a large wall of text that I've pasted onto my page inside a div with id "story". Each paragraph is actually on a single line in the html file, and each paragraph is separated by a single line. I want to make the wall of text more readable using bootstrap. I've set the css in a blog like format, is there any way to dynamically add </p><p> at every paragraph separation? 
            Asked
            
        
        
            Active
            
        
            Viewed 115 times
        
    0
            
            
         
    
    
        adnan_252
        
- 411
- 2
- 4
- 10
2 Answers
1
            var paragraphs = "your text".split(/\n\s*\n/);//since paragraphs are separated by
for(var i = 0; i < paragraphs.length; i++){   //a line, we need two \n here.
    var p = document.createElement("p");
    p.innerHTML = paragraphs[i].trim();
    document.querySelector("#story").appendChild(p);
}
//==============
//To get the text of an element (with new lines), you can do this:
document.querySelector("#story").childNodes[0].wholeText;
Maybe something like this? http://jsfiddle.net/DerekL/qv2GZ/
What you shouldn't do is replacing text inside a string and dumping it right into DOM. That's bad practice. That's why here I'm creating a p element instead of replace lines with </p><p>.
 
    
    
        Derek 朕會功夫
        
- 92,235
- 44
- 185
- 247
0
            
            
        I think that you should take a look here: How do I replace all line breaks in a string with <br /> tags?
And here: How to replace all occurrences of a string in JavaScript?
Remember that new line is simply \n. Then it is a matter of simple string replace. There is a huge research about it, and the question is a possible duplicate, so I think that is enough to answer :).
Best regards!
 
    
    
        Community
        
- 1
- 1
 
    
    
        Jacek Kowalewski
        
- 2,761
- 2
- 23
- 36
`s to the document.
– Derek 朕會功夫 Jul 24 '14 at 23:03in between the paragraphs now.
– adnan_252 Jul 24 '14 at 23:16