Shan Biswas Shan Biswas - 22 days ago 6
Javascript Question

trigger number of files selected using jquery

I am trying to catch the

number of files
user has selected using
JQuery
but my code does not work. JQuery is added and working. Every time the alert box display 1.



$('#product_image').change(function(){
var size = $("#product_image").length;
alert(size);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="file" class="" id="product_image" name="product_image" multiple>
</form>




Answer

Check length of files property of the DOM element.

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input type="file" class="" id="product_image" name="product_image" multiple>
</form>
JQuery
<script>
  $('#product_image').change(function() {
    var size = this.files.length;
    console.log(size);
  });
</script>