4

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

Community
  • 1
  • 1
user1879558
  • 51
  • 1
  • 4

1 Answers1

8

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.

Community
  • 1
  • 1
Ian
  • 50,146
  • 13
  • 101
  • 111