Jed Jed - 11 months ago 73
HTML Question

Load text from local .txt file into html textarea using JavaScript

I have a

element and a button that calls a
JavaScript function. I want this function to allow me to load the text from a .txt file on my local machine into the textarea. Any help with this would be greatly appreciated!

Answer Source

You can use the File and FileReader objects to read local files.

You can use an input element with type="file" to allow the user to select a file.

<input id="myFile" type="file"/>
<textarea id="myTextArea" rows="4" columns="20"></textArea>

After the user has selected a file, you can get the File object from the input element. For example...

var file = document.getElementById("myFile").files[0];

You can then use a FileReader object to read the file into the text area. For example...

var reader = new FileReader();
reader.onload = function (e) {
    var textArea = document.getElementById("myTextArea");
    textArea.value =;