resetting input file using $(this).val("") not working with IE8

internet-explorer, javascript, jquery

Solution

Refer to this: IE 9 jQuery not setting input value

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

So in your case:

$(this).replaceWith($(this).clone(true));

instead of the `$(this).val("");` line.

UPDATE:

In order to take advantage of browsers that allow you to modify the `input:file` element, I would use something like this:

$(".checkimgextension").on("change", function () {
    var $this = $(this);

    if (some conditions) {
        alert("wrong:...");
        $this.val("");
        var new_val = $this.val();
        if (new_val !== "") {
            $this.replaceWith($this.clone(true));
        }
    }
});

That way, it attempts to just set the value to empty at first, and if that doesn't succeed, use the `replaceWith` method.

Problem

Possible Duplicate: IE 9 jQuery not setting input value I would like to reset the input field if it matches some conditions using `$(this).val("");`: ``` $(".checkimgextension").on("change", function () { var file = $(this).val(); if (some conditions){ alert("wrong:..."); $(this).val(""); } ``` With Firefox the field is set to `""`, but with IE the field does not change as expected. Am using the right function `.val("")`?

Original source