2
I’m creating a module for node
and by performing some tests with the eventListener
of the kind change
in a field of the kind file
, the module is executed before a frame is inserted into the field.
exports.preview = (fileInput, whereToSee) => {
if (fileInput && fileInput[0]) {
let reader = new FileReader();
reader.onload = (event) => {
whereToSee.attr('src', event.target.result);
}
reader.readAsDataURL(fileInput[0]);
} else {
console.error('None!!'); // Mensagem de teste
}
}
The Code of Testing:
var upload = require('../index.js');
const file = document.querySelector('input[type="file"]');
file.addEventListener('change', upload.preview(this, document.querySelector('#blah')));
Just when I open the browser the message None!!
is displayed, ie the Event change
is being activated even before a file is selected.
I’m using the webpack
to create the bundle.js
from the archive main.js
which is the archive where the test codes are found:
webpack main.js Bundle.js
but that’s not the same as assigning a function to
exports
?– RFL
@Rafaelacioly no. You need to pass a function to Exports as you have, and that function returns another, which is the callback that will be used. Because the
addEventListener
needs a function, and as you have it, it runs,if
runs and theaddEventListener
does not even receive a/callback function in the arguments.– Sergio
that solved! thanks! but now I’m having another problem too! rs when I pass the
this
as a parameter ofupload.preview(this, ...)
the file input object is not returned, but an object (I don’t know which exactly), as it could to pass the ownfile
as parameter without having to select it again?– RFL
@Rafaelacioly it would not be case to pass
file
in time ofthis
?– Sergio
but I need to get the element after it’s changed
change
, if I pass thefile
the element is still empty.– RFL
@Rafaelacioly but it is kept, just because it receives a new value/file do not lose the reference. When the
change
was called, theinput
will have changed and you can read what you need from it.– Sergio