0

In a dynamically generated set of file upload controls:

<input type="file" name="archivos[]">
<input type="file" name="archivos[]">
<input type="file" name="archivos[]">
// ...

... I can easily count the non-empty ones:

// Works fine
var nonEmptyCount = $("input[type='file'][name='archivos[]'][value!='']").length;

However, when I try to count the empty ones (what I actually need) the selector never matches anything:

// Always zero
var emptyCount = $("input[type='file'][name='archivos[]'][value='']").length;

I can't believe I need this cumbersome code:

// Works but ugly
var emptyCount = $("input[type='file'][name='archivos[]']").length -
    $("input[type='file'][name='archivos[]'][value!='']").length;

What bit am I missing?

3 Answers 3

2

One solution is to check if value is empty inside an iterator like:

$("#findEmpty").on("click", function() {
  var count = 0;
  $(":file[name='archivos[]']").each(function() {
    if (this.value == "") {
      count++;
    }
  });
  alert(count);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" name="archivos[]" />
<input type="file" name="archivos[]" />
<input type="file" name="archivos[]" />
<input type="file" name="archivos[]" />
<input type="file" name="archivos[]" />
<input type="file" name="archivos[]" />
<input type='button' value='Check Empty' id='findEmpty' />

Sign up to request clarification or add additional context in comments.

2 Comments

Funny... The important bit seems to be using :file instead of [type='file']. With that little change, my original code seems to work as expected :-?
Whatever, .val() returns «the current value of the first element in the set».
0

You can filter them this way:

var emptyOne = $("input[type='file'][name='archivos[]']").filter(function () {
        return $(this).val() == "";
    });

$(function () {

    var emptyOne = $("input[type='file'][name='archivos[]']").filter(function () {
        return $(this).val() == "";
    });

    alert(emptyOne.length);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<input type="file" name="archivos[]">
<input type="file" name="archivos[]">
<input type="file" name="archivos[]">

EXAMPLE FIDDLE

Comments

0

I don't have an explanation (and I'll immediately accept any answer that provides one) but the root cause seems to be using the generic attribute equals selector to find the controls:

$("input[type='file']")

If I use the jQuery extension short-cut, everything else works as expected:

$("input:file")

As per the docs:

  • Both selectors should behave identically
  • [type='file'] should perform better

... so there must be a bug somewhere.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.