2

I am using an input element with type="file" and I want to cancel selection of something I choose.

I tried using

document.getElementById("pic1").value="";
document.getElementById("pic1").src="";

But it works only on Firefox.

Does anyone has a solution that works in IE and Chrome? Please no JQuery.

Kirby
  • 15,127
  • 10
  • 89
  • 104
Yonatan
  • 47
  • 1
  • 7

1 Answers1

3

This works in jsFiddle

  • Google Chrome 29

JavaScript:

$("#cancel").click(function(){
 document.getElementById("test").value = "";   
})

Here is a similar question: Clearing file input box in Internet Explorer

It's readonly in IE8 onwards, so you can't clear it. The simplest way around this security feature is to replace the element with a copy.

And here is a dirty solution to your problem jsFiddle from the same question:

JavaScript:

$("#cancel").click(function () {
    document.getElementById('test').parentNode.innerHTML = document.getElementById('test').parentNode.innerHTML;
})

It's dirty because it shouldn't work.

Community
  • 1
  • 1
skmasq
  • 4,470
  • 6
  • 42
  • 77
  • @Markasoftware I'm not using jQuery for input clearing. I'm using it for a different button to execute. – skmasq Sep 22 '13 at 19:00
  • you should use onclick is what i mean – markasoftware Sep 22 '13 at 19:04
  • @Markasoftware I'm more worried if the OP will even reply after this solves his/hers problem. :D – skmasq Sep 22 '13 at 19:06
  • hey thanks for helping, it's not working ..mabye i need to include any jquery? instead of clean the selection it send the form – Yonatan Sep 25 '13 at 12:39
  • @user2708548 http://stackoverflow.com/questions/1043957/clearing-input-type-file-using-jquery/1043969#1043969 here you go some jquery. – skmasq Sep 26 '13 at 02:32