Suppose I have input[type=file] element and I want to intercept onclick event and prevent file dialog from appearing if conditions are not met. Is it possible? And why, if - not?
Asked
Active
Viewed 7,184 times
2 Answers
13
Soufiane's code requires that you have a Javascript library called jQuery on your page. If you don't have it, you can get it at http://www.jquery.com or use something in plain Javascript:
HTML
<input type="file" id="openf" />
JS:
document.getElementById('openf').onclick = function (e) { e.preventDefault(); };

Joe
- 15,669
- 4
- 48
- 83
3
HTML:
<input type="file" class="openf" />
JS:
$('.openf').click(function(e){
e.preventDefault();
});

Soufiane Hassou
- 17,257
- 2
- 39
- 75
-
1Hm... how interesting, now that makes the whole thing even more quirky. I started from the other end and got confused. I was triggering click event on input[type=file] and couldn't intercept it. While this works as expected on physical click, for some reason it doesn't catch pseudo one triggered through $('.openf')[0].click()... But... only in FF. Chrome on the other hand doesn't show file dialog on $('.openf')[0].click() at all... – jayarjo Sep 08 '11 at 15:30