Skip to content
Advertisement

Read text file line by line and select random line javascript

I am trying to read a text file filepath = "data/words.txt" and randomly select a line to print in the console.

I tried to do:

JavaScript

However, I can’t seem to get the file to read.

Advertisement

Answer

if you want read a text file from the path you need run your code such as node.js environment; but if you using HTML and browser to pick file from input, continue .

add some html element to pick file and generate text from picked file :

JavaScript

and now add this js script :

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement