Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a link act as a file input

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?

like image 916
David542 Avatar asked Jul 10 '12 04:07

David542


People also ask

How do you put a link in an input tag?

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!

How do I create a Choose file button in HTML?

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!

How do I style a CSS file upload button?

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.


2 Answers

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.

like image 90
The Alpha Avatar answered Sep 19 '22 11:09

The Alpha


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> 
like image 44
Jamie Armour Avatar answered Sep 19 '22 11:09

Jamie Armour