Skip to content

Instantly share code, notes, and snippets.

@jenky
Last active August 5, 2016 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jenky/976c412e671046526bdfab7f81c7ef92 to your computer and use it in GitHub Desktop.
Save jenky/976c412e671046526bdfab7f81c7ef92 to your computer and use it in GitHub Desktop.
.btn-file {
position: relative;
overflow: hidden;
}
.btn-file input[type=file] {
position: absolute;
top: 0;
right: 0;
min-width: 100%;
min-height: 100%;
font-size: 100px;
text-align: right;
filter: alpha(opacity=0);
opacity: 0;
outline: none;
background: white;
cursor: inherit;
display: block;
}
<span class="btn btn-default btn-file">
Browse <input type="file">
</span>
$(document).on('change', '.btn-file :file', function() {
var input = $(this),
numFiles = input.get(0).files ? input.get(0).files.length : 1,
label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
input.trigger('fileselect', [numFiles, label]);
});
// Event handler
$(document).on('fileselect', '.btn-file :file', function(event, numFiles, label) {
var $this = $(this),
$label = $($this.data('label'));
if ($label.length) {
$label.append(label);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment