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.
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.
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.
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]')
Something using .filter() might work:
$('input:file').filter(function (file) {
return $(this).val().length > 0;
})