Ask your JavaScript questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

code modified so as to have zero listeners JavaScript

  • SOLVED

Original post is at https://javascriptquestions.com/Trying_to_read_a_file_into_a_variable_from_input/27744#answer_17998

I would like to have this js code modified so as to have zero listeners. I need a function to be called from the input: <input type="file" id="fileInput" onchange = "myNewFunction();" />

<input type="file" id="fileInput" />

<script>
// load file content from input
(function() {

// find the input by id
const fileInput = document.querySelector( "#fileInput" );

// listen for change
fileInput.addEventListener( "change", function() {

// check if a file has been selected
if ( this.files && this.files[0] ) {

// https://developer.mozilla.org/en-US/docs/Web/API/FileReader
const reader = new FileReader();

// wait for the contents of the file to be loaded
reader.addEventListener( "load", function( e ) {

// file data
const fileContent = e.target.result;
alert( fileContent );
});

// load selectd file
reader.readAsBinaryString( this.files[0] );
}
});
})();
</script>

Answers (1)

2019-11-12

Rainner Lins answers:

https://jsfiddle.net/rainnerlins/49yqzjph/

If it doesn't work, try loading the JS before the html.