0

I'm working on a little project and I have to cancel the event that normally happens when clicking on a file input : the display of a dialog box which allows the user to choose a file.

Here is my file input :

<input id="echangevignette" type="file">

I didn't find anything concerning cancelling this kind of events so that's why I need some help.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

0

Good old Event.preventDefault() should do the trick for you:

<html>
    <head>
        <meta charset="UTF-8">
        <title>Test</title>
    </head>
    <body>
        <script>
            document.addEventListener('DOMContentLoaded', () => { 
                document.getElementById('echangevignette').onclick = e => e.preventDefault();
            });
        </script>
        <input id="echangevignette" type="file">
    </body>
</html>

P.S. You can skip the DOMContentLoaded event listener if you load your scripts after the <input id="echangevignette" type="file">

Dima Mamchur
  • 1,104
  • 9
  • 8
0

This should do the trick....

HTML

<input id="echangevignette" type="file">

JS

document.getElementById("echangevignette").addEventListener("click", function(event){
 event.preventDefault()
});