Formatting a text file using jquery

Here is my question

I have a text file and I am reading the file using jquery.

code here

$(function() {
    $.get('files/james.txt', function(data) {
        $('.disease-details').html(data);
    }, 'text');
});

but I get only plain text and all formatting disappears.

I want to convert all tags to tag p. is it possible?

+4
source share
3 answers

You can do it:

$(function() {
     $.get('files/james.txt', function(data) {
        data = data.split("\n");
        var html;
        for (var i=0;i<data.length;i++) {
            html+='<p>'+data[i]+'</p>';
        }
        $('.disease-details').html(html);
    }, 'text');
});

The above text breaks to the next line (text "formatting") and wrap each piece in <p>.. </p>. Exactly as requested.

+5
source

, (, "" ).

, HTML. , <pre>, , , , .

:

$(function() {
    $.get('files/james.txt', function(data) {
        $('.disease-details').html($('<pre>').text(data)); // Text wrapped in <pre>
    }, 'text');
});
+2

You either have to pre-format the file using HTML tags, or simply get the data from a text file and do formatting on the client side (using templates or otherwise). May give you better pointers if you give an example of how the data in the file will look

0
source