I have done this in jQuery, to get filename from input file tag, With jQuery it works perfectly.
//jQuery('input[type=file]').on('change', prepareUpload);
document.getElementsByTagName('input[type=file]').addEventListener('change',prepareUpload1,true);
/*
//this works in jQuery
function prepareUpload(event)
{
var files = event.target.files;
var fileName = files [0].name
alert(fileName);
}
*/
/****Check it here ****/
// it does not work in javascript
function prepareUpload1(event)
{
var files = event.target.files;
var fileName = files [0].name
alert("fileName 2 : "+fileName);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" />
But I found Event.target does not work in IE, I tried to change it to java script addeventlistener, it did not work.
It throws error
Uncaught ReferenceError: input is not defined
It is working in jQuery but it is not working in JS, Due to IE issue I need to change it to JS.
Can some one help
I found the problem to be in
getElementsByTagName
method, you use this when you have a group of elements with the same tag name.Try this code below, it works
Below is the code if you want to do it for more than one element
This will work
The code above is correct for all but IE simply because IE doesn't like
event.target
- you needevent.srcElement
: https://developer.mozilla.org/en-US/docs/Web/API/Event/srcElementSo something like this should sort that out:
I couldn't make that work in the "Run Snippet" thing SO has, though, so here's how you can just get it using another button: