-1

I am trying to place a number from a local TXT file in the "let count = 0" instruction, so "let count = number.txt"

That way the counter will read the number placed on the txt file and perform the addition/subtraction operation.

Thank you!

<!DOCTYPE html>
<html>
<body>

<h1 class="counter-wolf-numbers">(..)</h1>
<button class="counter-wolf-minus">-</button>
<button class="counter-wolf-plus">+</button>


<script>
let counterDisplayElem = document.querySelector('.counter-wolf-numbers');
let counterMinusElem = document.querySelector('.counter-wolf-minus');
let counterPlusElem = document.querySelector('.counter-wolf-plus');

let count = 0;

updateDisplay();

counterPlusElem.addEventListener("click",()=>{
    count++;
    updateDisplay();
}) ;

counterMinusElem.addEventListener("click",()=>{
    count--;
    updateDisplay();
});

function updateDisplay(){
    counterDisplayElem.innerHTML = count;
};
</script>



</body>
</html>
Y M
  • 1
  • 2
  • where is this TXT file? on the server? on the client? – Jaromanda X Oct 28 '20 at 23:37
  • 1
    Does this answer your question? [How do I load the contents of a text file into a javascript variable?](https://stackoverflow.com/questions/196498/how-do-i-load-the-contents-of-a-text-file-into-a-javascript-variable) – blurfus Oct 28 '20 at 23:44
  • The TXT file is on the client – Y M Oct 29 '20 at 00:21

1 Answers1

1

You could use a file input to select the file you want, then use FileReader to read the contents of the file.

Edward Ellsworth
  • 455
  • 5
  • 12
  • Thank you, but it looks like this method can't load the file from a local path – Y M Oct 29 '20 at 00:20
  • Correct. You would have to select the file. If you need to access a file simply by file path, you'll need to use something like node's file system. https://nodejs.org/api/fs.html – Edward Ellsworth Oct 29 '20 at 00:29