5

I have the following input file tag:

<input type="file" id="handlerxhr1" />

In mozilla when I run the following jQuery code:

var input = $('#handlerxhr1')[0];
        $('#upload').click(function() {
            alert(input.files[0]);

        });

I get response: [object File] (which is good).

But in IE I get 'input.files.0 is undefined'

What am I doing wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
ShaneKm
  • 20,823
  • 43
  • 167
  • 296
  • 1
    try alert(typeof(input.files)); in IE – Umair A. Feb 15 '11 at 07:37
  • It might be affected by the way IE handles JS differently than firefox. The click event you have putten on your upload button fires after the upload is done on firefox, and before its done on IE. – Jan Dragsbaek Feb 15 '11 at 07:38
  • can't be. because click event i declared var input when document.ready and after it's loaded i click '#upload' button – ShaneKm Feb 15 '11 at 07:40

2 Answers2

6

IE doesn't support .files[0] property, whereas FF does. See http://www.w3.org/TR/FileAPI/ for more details

Lalit
  • 4,897
  • 7
  • 32
  • 36
4

This seems good enough...

$(function() {
    var input = $('#handlerxhr1')[0];         
    $('#upload').click(function() {             
        alert(input);          
    }); 
});

Not sure if your were after something like this though:

$(function() {
    var input = $('#handlerxhr1')[0];         
    $('#upload').click(function() {             
        var x = $('input[type=file]:eq(0)');
        alert(x);
    }); 
});
bcm
  • 5,470
  • 10
  • 59
  • 92
  • 2
    `$('#handlerxhr1')[0]` is the same as `$('#handlerxhr1')` because jquery returns an array of matching elements. It doesn't do the same thing as `files[0]`. – NibblyPig Dec 05 '12 at 17:03