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
  • 197,815
  • 30
  • 248
  • 265
Mario
  • 1,246
  • 2
  • 18
  • 38

1 Answers1

1

Line breaks in HTML are <br>. So:

$( "#MyDiv" ).html(data.split("\n").slice(-5).join("<br>"));
j08691
  • 197,815
  • 30
  • 248
  • 265
  • 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