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?

3
  • 1
    try alert(typeof(input.files)); in IE Commented Feb 15, 2011 at 7: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. Commented Feb 15, 2011 at 7:38
  • can't be. because click event i declared var input when document.ready and after it's loaded i click '#upload' button Commented Feb 15, 2011 at 7:40

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

2 Comments

Well, what is a cross browser option?
is there any external library for handling files in IE9?
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);
    }); 
});

1 Comment

$('#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].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.