javascript function provides file size of only the first FileUpload, not the others

Go To StackoverFlow.com

1

I am making file input boxes through a javascrpit function, if the user wants to Attach more files, The function is

<script type="text/javascript">
var upload_number = 1;
var attachmentlimit = 5;
function addFileInput() {

var d = document.createElement("div");
var file = document.createElement("input");
file.setAttribute("type", "file");
file.setAttribute("name", "attach[]");
d.appendChild(file);
document.getElementById("moreUploads").appendChild(d);
upload_number++;
if(upload_number == attachmentlimit) {
      document.getElementById('moreUploadsLink').style.display='none';
    }

    }
    </script>

I am also checking the size of the File input at client through javascript function,

  <script type="text/javascript">
  var myFile = document.getElementById('attach');
    myFile.addEventListener('change', function() {
   var  size = this.files[0].size;
   alert(size,'File Size Exceeds'); 
   });
  </script>

The problem is, the above javascript function, gives the Size alert of only the first File Upload box, and not the others. Any one who tell how to make this to provide sizes of all file uploads.

2012-04-04 17:31
by Hanya Idrees


1

Add the event listener to the file somewhere around d.appendChild(file). Otherwise, if you never tell JS to listen for the event, how can you expect it to?

2012-04-04 17:35
by Niet the Dark Absol
how to do it, can u tell m - Hanya Idrees 2012-04-04 17:39
Thank you, i done i - Hanya Idrees 2012-04-04 17:44
Ads