Like a lot of people, I'd like to customize the ugly input type=file
, and I know that it can't be done without some hacks and/or javascript
. But, the thing is that in my case the upload file buttons are just for uploading images (jpeg|jpg|png|gif), so I was wondering if I could use a "clickable
" image which would act exactly as an input type file (show the dialog box, and same $_FILE on submitted page).
I found some workaround here, and this interesting one too (but does not work on Chrome =/).
What do you guys do when you want to add some style to your file buttons? If you have any point of view about it, just hit the answer button ;)
its really simple you can try this:
This is my method if i got your point
HTML
jQuery
This works really well for me:
With styling:
Basically the for attribute of the label makes it so that clicking the label is the same as clicking the specified input.
Also, the display property set to none makes it so that the file input isn't rendered at all, hiding it nice and clean.
Tested in Chrome but according to the web should work on all major browsers. :)
EDIT: Added JSFiddle here: https://jsfiddle.net/c5s42vdz/
Working Code:
just hide input part and do like this.
I would use SWFUpload or Uploadify. They need Flash but do everything you want without troubles.
Any
<input type="file">
based workaround that tries to trigger the "open file" dialog by means other than clicking on the actual control could be removed from browsers for security reasons at any time. (I think in the current versions of FF and IE, it is not possible any more to trigger that event programmatically.)You can put an image instead, and do it like this:
HTML:
JQuery:
CAVEAT: In IE9 and IE10 if you trigger the onclick in a file input via javascript the form gets flagged as 'dangerous' and cannot be submmited with javascript, no sure if it can be submitted traditionaly.