Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting text file using jquery

Here is my question,

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

the code is here

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

but I am getting only plain text and all formatting is disappearing.

I want to convert all the enter in to p tag. is it possible?

like image 693
Sujith Wayanad Avatar asked Mar 22 '23 00:03

Sujith Wayanad


1 Answers

You can do this :

$(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 splits up the text by new line (the text-"formatting") and wraps each chunk into a <p> .. </p> .. Exactly as requested.

like image 175
davidkonrad Avatar answered Apr 02 '23 11:04

davidkonrad