Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
-2
rated 0 times [  2] [ 4]  / answers: 1 / hits: 16468  / 11 Years ago, tue, june 11, 2013, 12:00:00

I know that is simple question but I need an advice from experienced people.



I have 3 input type file like:



<input type=file name=1-files />
<input type=file name=2-files />
<input type=file name=3-files />


I select all inputs (on my page I have also other inputs type file) which name ends with -files ( I wrote in Google Chrome console):



$(input[type='file'][name*='-files']).length


Ok. I select a file using 1-files input. After that, I run the following code in Google Chrome console:



$(input[type='file'][name*='-files']:empty).length


I expect to be 2 but appears 3.



Can you tell me why ?



I want to get all elements of input type file which values are empty. I used in short way the selector :empty but it seems not working properly.



Of course, I could use



var empty_count = 0;
$.each($(input[type='file'][name*='-files']), function(k, v){
if($(this).val() === '')
empty_count++;
});


But I want the shortest way to do this without $.each.



Thank you


More From » jquery

 Answers
7

It matches three because :empty matches an element with no descendants, and am input element by definition cannot have descendants (so all are empty, and therefore match the :empty selector).



To find those elements without selected files, I'd suggest:



$(input[type='file'][name*='-files']).filter(function (){
return !this.value
}).length;


References:




[#77691] Monday, June 10, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
quinlanhenryf

Total Points: 743
Total Questions: 93
Total Answers: 118

Location: Australia
Member since Sat, May 27, 2023
1 Year ago
;