I am using the following jsfiddle code to determine uploaded image file's attributes. There are multiple file upload elements. I wish to know the current id of the element that has called the function so that I can use switch function. For example, if id = file1, then do this, elsif it is file2, then do this and so on. I used $(this).attr("id"), this.id; but none works.
$(window).load(function(){
var _URL = window.URL || window.webkitURL;
function displayPreview(files) {
var reader = new FileReader();
var img = new Image();
reader.onload = function (e) {
img.src = e.target.result;
fileSize = Math.round(files.size / 1024);
img.onload = function () {
$('#preview').append('<img src="' + img.src + '">');
}
};
reader.readAsDataURL(files);
}
$("#file1").change(function() {
var file = this.files[0];
displayPreview(file);
});
$("#file2").change(function() {
var file = this.files[0];
displayPreview(file);
});
$("#file3").change(function() {
var file = this.files[0];
displayPreview(file);
});
});
This is the html code:
<input type="file" id="file1" /></br>
<input type="file" id="file2" /></br>
<input type="file" id="file3" /></br>
<div id="preview"></div>
So, how do I know the id of the current element which has called the function?