Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 631
user1879558 Avatar asked Feb 18 '23 06:02

user1879558


1 Answers

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.

like image 164
Ian Avatar answered Feb 20 '23 19:02

Ian