Skip to content Skip to sidebar Skip to footer

Uncaught Typeerror: Cannot Read Property '0' Of Undefined

I've been getting this weird error and I just can't figure it out Here is my current code: function addBackground() { var penguin_id = getPenguinID(); var file = $('#image_f

Solution 1:

Instead of $('#image_form')[0].files[0]; try with $('#image_form').prop('files')[0]; It worked for me.

Solution 2:

In jQuery, using an id selector (such as $('#image_form')) will only yield the first result, so the [0] next to it is unneeded. (Forget that, an id selector will always yield a collection no matter how many items there are - Thanks Leo for bringing it up.) Moreover, it has no property files, which will yield yet another error.

What do you want to achieve here?

Solution 3:

your problem lies in the files[0] I believe. perhaps you are trying to get the value in the input field? you're acquiring a jquery set of the firm, on which the files property is undefined. $("#image_form").find("input").val() should give you the input box value, though there may be a more appropriate way when dealing with forms

Post a Comment for "Uncaught Typeerror: Cannot Read Property '0' Of Undefined"