With jQuery how do I count how many fields of my array field[] are not empty ?
Sample of the input field
<input type="file" name="arquivo[]" id="arquivo">
I was trying to make something up using map and get but it is not coming along well:
var total = 0;
var count = $('#arquivo[value!=""]').map(function() { total = total+1; }).get();
But no matter how many fields I have filled it always end up with the value of 1 and if I have no fields filled 0.
You can just find the
lengthof the jQuery object, which returns the number of matched elements:But you do know that this will always return
0or1? Theidproperty is unique to only one element, so you can’t reuse it multiple times.For example:
When you run:
It returns
1, because only one element should exist with theidoffoo.Now try this:
When you run:
It returns
2. Why? Becauseclasscan be reused many times.What are you trying to do? Can you post a scenario with multiple input fields?