Skip to content Skip to sidebar Skip to footer

Empty Input Type File Doesn't Work In Ie

I wrote this simple javascript code: $('input[type='file']').attr('value', ''); It works in Firefox and Chrome but not in IE (I tried in IE9) What's the good approach to empty the

Solution 1:

There are some restriction on input type = file

You can read here

This seems to be working in IE8, IE9

$("input[type='file']").replaceWith($("input[type='file']").clone(true));

To test in IE8 I changed the compatibility mode in developer tool to IE8

Edit:

As for many IE hacks I think this is better

if($.browser.msie){
        $("input[type='file']").replaceWith($("input[type='file']").clone(true));
    } else {
        $("input[type='file']").val('');
    }

Solution 2:

jQuery docs says:

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. In addition, .attr() should not be used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the .prop() method.

Use .prop()

Post a Comment for "Empty Input Type File Doesn't Work In Ie"