How input:file is not triggered by a click.

< input type= "file" > how to use js emulation to trigger a file selection pop-up box without clicking the trigger. (you can"t click on other elements either).

Nov.17,2021

jquery triggers
$('input'). Click ()
can also be used natively
dom = document.getElementById (''). Click ();
) or
var event = document.createEvent ('MouseEvents')
event.initMouseEvent (' click',false,false)
dom.dispatchEvent (event)


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

var $input = document.querySelector('-sharpupload');
$input.onclick = function() {
    console.log('a');
}
$input.click();

execute the above code in chrome, and the console will print out the contents of the console statement, but will not pop up the window that selects the file. Execute document.querySelector ('- sharpupload'). Click () ) directly in the console and the window will pop up.
there will be a message in firefox saying that a pop-up window is blocked, and if you choose allow, you will be able to pop up the window that selects the file.
this should be due to the browser's security considerations.

Menu