An empty input type file does not 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 is a good approach to remove input file type value?

thanks

+3
source share
2 answers

There are some restrictions on input type = file

You can read here

It seems to work in IE8, IE9

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

For testing in IE8, I changed the compatibility mode in the developer tool to IE8

Edit:

As with many IE hacks, I think it's better

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

jQuery docs says :

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

Use . prop ()

+1
source

All Articles