Nitin Verma
Nitin Verma

Reputation: 83

if condition works only once when selecting more than limit files

I have to refresh the page if i want this alert message again.

document.getElementById("images").addEventListener("change", function() {
  var length = this.files.length;
  if (length > 2) {
    alert("you can not select more than 2 files")
  }
});
<input class="control4" name="images[]" id="images" multiple="multiple" name="images" type="file" accept="image/jpg, image/jpeg,image/png" />

Upvotes: 1

Views: 47

Answers (1)

Chris Li
Chris Li

Reputation: 2671

it works for me, are you choosing the same file on the second time? Change event only fires when the value changes, it can be fixed by setting value of input to empty string.

document.getElementById("images").addEventListener("change", function() {
  var length = this.files.length;
  if (length > 2) {
    this.value = "";
    alert("you can not select more than 2 files");
  }
});
<input class="control4" name="images[]" id="images" multiple="multiple" name="images" type="file" accept="image/jpg, image/jpeg,image/png" />

Upvotes: 1

Related Questions