0

I have a problem with a little site (it's intended to work as a local site) I try to create. I want it to print text from local txt file onto the page. I want it to display it like this one,

<script type="text/javascript">
var arr = ['Heading 1','Para1','Heading 2','Para2','Heading 3','Para3'];
var result = arr.map((val, i) => i%2===0 ? `<h2>${val}</h2>` : `<p>${val}</p>`).join('');
document.getElementById('test').innerHTML = result ;
</script>

but I want it to do it from a file, like this one

<script>
 var fileInput = document.getElementById('inputfile');
 fileInput.addEventListener('change', function(e) {
     var file = fileInput.files[0];
     var reader = new FileReader();
     reader.onload = function(e) {
         document.getElementById('output').innerText = reader.result;
 };
     reader.readAsText(file);   
 });
 </script>

I tried to merge them together like this

   <script>
    var fileInput = document.getElementById('inputfile');
    fileInput.addEventListener('change', function(e) {
        var file = fileInput.files[0];
        var reader = new FileReader();
        reader.onload = function(e) {
            document.getElementById('output').innerHTML = reader.result.split("\n").map((val, i) => i%2===0 ? <h2>${val}</h2> : <p>${val}</p>).join('');
        };
    reader.readAsText(file[0]);   
 });
 </script>

But something is still not working right (after choosing the file to read, it does nothing) and I am not sure what am I doing wrong. I am green in javascript, so I would appreciate any help in that matter.

7
  • 1
    does the user uploads a txt file to the input field? Commented Oct 26, 2021 at 11:41
  • 1
    I just tried and your 2nd example works for me - do you get anything into your console? Also is your script the last thing on the page? Commented Oct 26, 2021 at 11:42
  • @vsync Yes, user inputs the text into the file Commented Oct 26, 2021 at 11:55
  • 1
    well, this question is not about files at all but about parsing text into segments. The file upload part here is irrelevant Commented Oct 26, 2021 at 11:58
  • 1
    I think the issue is still with with the file reading; I added an answer that I tested locally Commented Oct 26, 2021 at 12:13

1 Answer 1

1

Actually, now that I read that again - the only issue with your example is you were using file[0] instead of file

    <input type="file" id="inputfile" />
    <p id="output"></p>
    <script>
    var fileInput = document.getElementById('inputfile');
    fileInput.addEventListener('change', function(e) {
        var file = fileInput.files[0];
        var reader = new FileReader();
        reader.onload = function(e) {
            document.getElementById('output').innerHTML = reader.result.split("\n").map((val, i) => i%2===0 ? `<h2>${val}</h2>` : `<p>${val}</p>`).join('');
        };
     reader.readAsText(file);   // HERE! 
    });
</script>

Sign up to request clarification or add additional context in comments.

5 Comments

@To be honest, that one is still not working as I wanted it to (nothing is printed onto page after file selection, just like before), but the first one was working exactly as I needed it to, so thanks a lot
@OnionOfDoom Allright, hmm - I think there is still something then that is not perfect in this solution. I think I am missing or misunderstanding something, but I'm glad you got it figured out. May your layers be strong and your core full of doom - good luck
@OnionOfDoom I think I fixed it, try to run the snippet now - I was missing backticks (`) for the template literal... whoops!
Yup, that one works as well. Good to know that I was stuck for a few days on 4 missing backticks and and unnecessery "[0]". Looks like there is a long way ahead of me then, but still, thanks a lot for Your help
Ce'st la programming @OnionOfDoom - Especially with JavaScript :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.