Last active
August 5, 2016 03:36
-
-
Save jenky/976c412e671046526bdfab7f81c7ef92 to your computer and use it in GitHub Desktop.
Bootstrap input file button (Credit: http://www.abeautifulsite.net/whipping-file-inputs-into-shape-with-bootstrap-3/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<span class="btn btn-default btn-file"> | |
Browse <input type="file"> | |
</span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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