0

I'm making a form, and as you know some some types of forms are harder to style than others. For instance <input type="file">, I'd like to somehow make a fake button so when you click the fake button, it's really clicking the input button.

I've tried:

$("#inputFile").click();

But as you suspect, this didn't work.

Is this possible?

LOTUSMS
  • 10,317
  • 15
  • 71
  • 140
  • 1
    http://ericbidelman.tumblr.com/post/14636214755/making-file-inputs-a-pleasure-to-look-at – Amadan Apr 03 '14 at 02:07
  • Obviously, the ability of a browser to upload local files to a website, under the control of that website and without the user's knowledge, is deliberately restricted. – Michael Lorton Apr 03 '14 at 02:16
  • The answer to this question http://stackoverflow.com/questions/6461252/custom-upload-button/6461518#6461518 does what you are looking for. – natedavisolds Apr 03 '14 at 02:16

3 Answers3

4

You can trigger click event of input element using .trigger() when click on the button:

$('button').click(function() {
    $('input[type="file"]').trigger('click');    
});

Fiddle Demo

Felix
  • 37,892
  • 8
  • 43
  • 55
0

@felix's answer is right. In addition,

The example is given on jQuery API page as well .

Please check the 3rd example

We can also trigger the event when a different element is clicked:

$( "#other" ).click(function() {
    $( "#target" ).click();
});
Nis
  • 1,469
  • 15
  • 24
-1

Here is an example of what you are looking for.

Custom Upload Button

I repeat the answer here:

The trick is to turn the opacity of the input to 0 and then change the background underneath it to the button style that you want.

Given this html:

<div class="file_button_container"><input type="file" /></div>

Use this css:

  .file_button_container,
  .file_button_container input {
       height: 47px;
       width: 263px;
   }

   .file_button_container {
       background: transparent url(https://i.stack.imgur.com/BT5AB.png) left top no-repeat;
   }

   .file_button_container input {
       opacity: 0;
   }

Here it is in action: http://jsfiddle.net/t6EKv/1/

Community
  • 1
  • 1
natedavisolds
  • 4,305
  • 1
  • 20
  • 25