Question

I'm sure this question has been asked before, so I'm sorry if it's a duplicate. I'm pulling in data from a text document using AJAX. How would I separate the paragraphs within the AJAX call so that the paragraphs are split on a paragraph break. I'm showing a change in the html document, but I'm not receiving the results I want. The paragraphs are not splitting up, instead, the paragraph break is simply being removed. FYI, I'm not very experienced with AJAX.

JAVASCRIPT

$(document).ready(function(){
    $.ajax({
        type: "GET",
        dataType: "text",
        url: "text.txt",
        success: function(txt){
            var str = (txt);
            str = (str.split("\n"));
            $("#test").append(str);
        }
    });
})

TEXT

With crime at an all time high, the need for home security has become an even
greater necessity. Although it may not happen to you, and we all hope it never
does, the truth is, break-ins happen. The best way to deal with this dangerous
situation is to install a security system that will help prevent thieves from
entering your home and causing harm to your family or taking your valuables.

Our monitoring station operates 24 hours a day 7 days a week and 365 days a year.  
We   offer security that you can rely on, which means that you can be assured that
your home is being watched by our professionals. Our monitoring service is
offered at a low price, you shouldn't look at it as a cost, but more of an 
investment.

This is also the desired output I want, however, This is what I'm getting. I've capitalized where the paragraphs are supposed to split.

OUTPUT

With crime at an all time high, the need for home security has become an even
greater necessity. Although it may not happen to you, and we all hope it never 
does, the truth is, break-ins happen. The best way to deal with this dangerous 
situation is to install a security system that will help prevent thieves from 
entering your home and causing harm to your family or taking your valuables.OUR 
monitoring station operates 24 hours a day 7 days a week and 365 days a year. 
We offer security that you can rely on, which means that you can be assured that 
your home is being watched by our professionals. Our monitoring service is 
offered at a low price, you shouldn't look at it as a cost, but more of an 
investment.

Thanks for the help.

Was it helpful?

Solution

Simply replace you code split by a replace so that newline are replace by the HTML equivalent
as follows:

function(txt){
    stri = txt.replace(/\n/g, "<br/>");
    $("#test").append(stri);
}

The replace uses the regular expression /\n/ to detect newline character \n

Note: the g suffix stands for globally, eg. process all occurrences of \n

OTHER TIPS

var str = text.split('\n\n').map(function(p) { return '<p>' + p + '</p>'; }).join('\n\n')

or, if you need it to work in obsolete browsers,

var str = $.map(text.split('\n\n'), function(p) { return '<p>' + p + '</p>'; }).join('\n\n')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top