Possible Duplicate:
jQuery : simulating a click on a <input type=“file” /> doesn't work in Firefox?
Is it possible to have a file button without an input beside it by default? Ideally all I want is a button that lets the user navigate to a file without showing what they selected prior to an upload. I'll submit the form by the following after a user chooses a file:
$("#file").change(function() {
$("#update_button").trigger('click');
});
I'm sure this must be possible with some css or jquery magic...
If I remember correctly, HTML5 allows you to call the
click
method on a hidden input element to show the file dialog via a custom button (why not just make it work without an element, I don't know). Unfortunately not all currently in use browsers support this yet, so you're stuck with styling a file input to look like a button.This is a hilariously ugly but ingenious CSS hack I came across on a site once (probably imgur):
And the HTML to go with it:
What it does is it makes the file input itself enormous (by changing the font size (!?)) to ensure it covers the button, and then cuts off the excess with
overflow: hidden;
. This may not work for absolutely enormous buttons.An option to look at is here:
http://shauninman.com/archive/2007/09/10/styling_file_inputs_with_css_and_the_dom
This allows for more customization of an
<input>
fieldYou could simply hide the
input
button withvisibility: hidden;
, and attach a click event handler to the other button:HTML:
CSS:
JavaScript:
Here's the fiddle: http://jsfiddle.net/tCzuh/
If you set the opacity to 0, then you can add another div underneath it that looks like a button. You can style this any way you like then.
Working code example below: