3

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 input type file value ?

Thank you

Snake Eyes
  • 16,287
  • 34
  • 113
  • 221
  • 1
    possible duplicate of [how to reset (clear) file input](http://stackoverflow.com/questions/9011644/how-to-reset-clear-file-input) – Quentin Apr 26 '12 at 05:50

2 Answers2

10

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('');
    }
mprabhat
  • 20,107
  • 7
  • 46
  • 63
1

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()

maialithar
  • 3,065
  • 5
  • 27
  • 44