I have a link,
<a id="upload-file">Upload your photo</a>
And if clicked, I want it to act as an browse file input
<input id="upload-file" type="file"/>
How would I accomplish this?
The <input type="url"> defines a field for entering a URL. The input value is automatically validated before the form can be submitted. Tip: Always add the <label> tag for best accessibility practices!
The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!
Styling the upload file button By default, the Choose file button has a plain user-agent style. To style the button with CSS you should use the ::file-selector-button pseudo-element to select it. It is supported in all modern browsers.
HTML
<input id="upload" type="file"/> <a href="" id="upload_link">Upload your photo</a>
CSS
#upload{ display:none }
JS
$(function(){ $("#upload_link").on('click', function(e){ e.preventDefault(); $("#upload:hidden").trigger('click'); }); });
DEMO.
HTML Only
Here's a pretty simple answer that works with no CSS, Javascript/jQuery and doesn't rely on any framework infrastructure.
<label> <input type="file" style="display: none;"> <a>Upload Photo link</a> </label>
or even simpler
<label> <input type="file" style="display: none;"> Upload Photo link </label>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With