views:

55

answers:

2

I have a list of file input boxes and I want to know if any of them have a value using a single selector statement. Can I do something like this:

$('input:file[value!=null]')

or something similar.

+2  A: 

Update: Please see the comment from @bobince. This is a bug with the jQuery's Sizzle selector in that the line between attributes and properties is blurred. The relevant lines from jQuery's source:

elem[ name ] != null ?
    elem[ name ] :
    elem.getAttribute( name ),

It looks up the object's property by that name first, and if it fails, falls back to getAttribute(). It is better to filter on val() as suggested by @Aaron.


Leaving the old answer here for the benefit of others that may bump across this innocent looking selector.

null will be interpreted as a string. So a chosen file with name "null" will be excluded. Instead just select file elements where the value attribute exists. value should only be defined if the user has chosen a file for that field.

$('input:file[value]')
Anurag
Brilliant, works like a charm. Thank you! I'll mark as accepted as soon as the cool down is over.
Matt
This will only select `<input>`​s with a `value="..."` *attribute* present — except on IE, due to a bug in how it handles form field values. You probably won't have put a `value` attribute on any of your file inputs, because the browser doesn't do anything with the default value on a file input. If you are looking to style file inputs differently based on whether or not a file has been chosen in them, you'll have to do it Aaron's way.
bobince
@bobince, specifying the `:file` filter does some jQuery magic here (i don't know what to be honest), but it makes jQuery look at `<object>.value` (works on my Chrome) - some tests here - http://jsfiddle.net/ez4Xe/
Anurag
Specifying `:file` makes jQuery use the Sizzle selector engine instead of querySelectorAll, since it's not a standard CSS selector. The ‘magic’ in Sizzle, using object properties where attributes some have been used (line 3277 in jquery-1.4.2) is nothing more or less than a bug, which you shouldn't rely on. Related bugs #6004, #3245, #2662, #4244, #5480...
bobince
@bobince, thanks for the hint on jquery's source. the order in which jQuery searches these attributes (in this particular case) is to first try the object property `value`, and if its falsy, then try getAttribute(name). So yes, it is a bug. Updating my answer now.
Anurag
+3  A: 

Something using .filter() might work:

$('input:file').filter(function (file) {
                  return $(this).val().length > 0;
                })
Aaron Mc Adam
Presumably you mean `val().length>0`.
bobince
oh yeah, of course sorry, I'll edit it
Aaron Mc Adam