I have a form and I am adding an addEventListener()
to the form. I wanted to call a function from within the addEventListener()
but I am getting an Uncaught TypeError
at the function call.
Code 1
$(document).ready(function(){
function uploadFile(){
alert("comes here");
}
uploadForm.addEventListener('submit', function(e) {
var uploadFile = document.getElementById('upload-file').files;
e.preventDefault();
uploadFile();
});
});
But if I change the above code to
Code 2
$(document).ready(function(){
var test = function uploadFile(){
alert("comes here");
}
uploadForm.addEventListener('submit', function(e) {
var uploadFile = document.getElementById('upload-file').files;
e.preventDefault();
test();
});
});
It works. I want to know why. It maybe something associated with the scope. But from whatever I know, I think the function(uploadFile()
) and the function reference(test
) has the same scope.
I am a self taught programmer so I don't know what to google to understand why this happens. The closest I got is this , function naming
As an answer, I am just looking for some terms which I can google to understand why this happens.