0

I am trying to get the last 5 lines from a large file using JavaScript

I've tried a lot to do it. Like this:

<script>
    $.get( "myfile.txt", function(data) {
      $( "#MyDiv" ).html(data.split("\n").slice(-5).join("\n"));
    })
</script>
<div id="MyDiv"></div>

Which should show the contents into MyDiv but it show them on one line.

j08691
  • 204,283
  • 31
  • 260
  • 272
Mario
  • 1,374
  • 6
  • 22
  • 48

1 Answers1

1

Line breaks in HTML are <br>. So:

$( "#MyDiv" ).html(data.split("\n").slice(-5).join("<br>"));
j08691
  • 204,283
  • 31
  • 260
  • 272
  • 1
    That did it. I was adding `
    ` in `data.split()` too :/ Thank you very much! Accepting the answer once I can.
    – Mario Aug 27 '18 at 16:50