7
A question, why does it work:
$("input[type='file']")[0].files[0]
and that’s not?
$("input[type='file']").files[0] // TypeError: $(...).files is undefined
7
A question, why does it work:
$("input[type='file']")[0].files[0]
and that’s not?
$("input[type='file']").files[0] // TypeError: $(...).files is undefined
10
With Wallace replied, he returns a jQuery Object. Thus, you are accessing the first ( and probably unique, in your case ) DOM element that returned from the object.
You can access an element as follows:
$("input[type='file']")[0]
or
$("input[type='file']").get(0)
There is also the function .first(), which will return the first DOM element of the object returned by the selector:
$("input[type='file']").first()
NOTE: The . first() function will create a new jQuery Object with the first element.
@Edit
As the tip commented by Guilherme Lautert, can also access an index through the selector :eq():
$("input[type='file']:eq(0)")
5
When you do that $("input[type='file']")[0]
, you are accessing the object HTMLInputElement
, which is native to javascript.
In this element, there is the property files
.
Example:
HTMLInputElement.prototype.hasOwnProperty('files'); // true
In the case of $("input[type='file']")
you are returning the instance the object of jQuery
, who does not own this property.
4
Basically the same answer from Wallace, though I’ll explain it another way.
The .files
is a native property of the Javascript DOM API, so it will only be accessible on when you use document.getElementById
and document.querySelector
, for example:
document.querySelector("input[type=file]").files
When you do it:
$("input[type=file]")[0] //ou .get(0)
You transform (extract) an object to GIFT, like I got caught up with document.querySelector
, doing the .files
be accessible.
So .files
would only be accessible like this $(...).files
if this property existed in jQuery.
Note:
document.getElementsByName
,document.getElementsByTagName
anddocument.querySelectAll
returns objectsarrays
then.files
will only be available if you access one of the items in thisarray
.
Is there a way to make this accessible, through the jQuery.fn.
jQuery’s, it would be like this:
jQuery.fn.extend({
"files": function() {
var el = this.get(0);
var files = el ? el.files : false;
return files ? files : [];
}
});
And using it must be something like:
console.log($("input[type=file]").files());
Note that he will return one array
with the files selected and only from the first element of the "jQuery object list".
4
Why Jquery will search for One or + occurrences of input[type='file']
, will soon list the results in a Object by the keys ([0][1][2],etc)
.
Actually that’s not quite it. If there was an object with captured by jQuery, there would be no property files
likewise.
Yes, you did present much more complete. But my explanation still makes sense, however, hyper simplified, right?
No, no. The problem is not the amount of results. The problem is the object being returned. That’s what I wanted to present in my question. One returns object
(which is jQuery) and another is returned the HTMLInputElement
, which is the input file
jQuery independent (it is javascript native). His answer leads to understand that the problem is that jQuery has returned more than one element, and this is not the truth. It could return up to 100. If he used the [0]
ahead always return the Htmlinputelement
That’s why I gave the -1, because it doesn’t answer the question
Great! Thank you, I think it’s much clearer now.
Yes, good. I was more concerned with understanding than with this.
Let’s go continue this discussion in chat.
Browser other questions tagged javascript jquery
You are not signed in. Login or sign up in order to post.
can also be used
$("input[type='file']:eq(0)")
.– Guilherme Lautert
Thank you for the reminder! Added to the reply.
– Marcelo de Andrade