Home >Web Front-end >JS Tutorial >How can I trigger a hidden file input box with jQuery?
Triggering File Input with jQuery
In an effort to initiate an upload box using jQuery, you have utilized the method:
$('#fileinput').trigger('click');
However, it has proven unsuccessful. This presents a question: How can one successfully trigger a file input box using jQuery?
Security Restrictions and Workaround
The failed attempt can be attributed to security restrictions. It is important to note that security restrictions are imposed when the element is rendered hidden (either through display:none or visibility:hidden).
To circumvent this restriction, a workaround is to position the element outside the viewport by setting position:absolute and top:-100px. This displaced positioning allows the triggering mechanism to function effectively.
Here is an example of such a solution: http://jsfiddle.net/DSARd/1/
This approach essentially tricks the browser into perceiving the element as being outside the viewport, thus allowing the triggering mechanism to operate as intended.
The above is the detailed content of How can I trigger a hidden file input box with jQuery?. For more information, please follow other related articles on the PHP Chinese website!