Skip to content

Instantly share code, notes, and snippets.

@fguillen
Last active February 22, 2019 18:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save fguillen/4753912 to your computer and use it in GitHub Desktop.
Save fguillen/4753912 to your computer and use it in GitHub Desktop.
Pretty 'input[type="file"]' for Bootstrap addicts
// Based in: http://duckranger.com/2012/06/pretty-file-input-field-in-bootstrap/
// Version: 0.0.3
// Compatibility with: Bootstrap 3.2.0 and jQuery 2.1.1
// Use:
// <input class="nice_file_field" type="file" data-label="Choose Document">
// <script> $(".nice_file_field").niceFileField(); </script>
//
(function( $ ) {
$.fn.niceFileField = function() {
this.each(function(index, file_field) {
file_field = $(file_field);
var label = file_field.attr("data-label") || "Choose File";
file_field.css({"display": "none"});
nice_file_block_text = '<div class="input-group nice_file_block">';
nice_file_block_text += ' <input type="text" class="form-control">';
nice_file_block_text += ' <span class="input-group-btn">';
nice_file_block_text += ' <button class="btn btn-default nice_file_field_button" type="button">' + label + '</button>';
nice_file_block_text += ' </span>';
nice_file_block_text += '</div>';
file_field.after(nice_file_block_text);
var nice_file_field_button = file_field.parent().find(".nice_file_field_button");
var nice_file_block_element = file_field.parent().find(".nice_file_block");
nice_file_field_button.on("click", function(){ console.log("click"); file_field.click() } );
file_field.change( function(){
nice_file_block_element.find("input").val(file_field.val());
});
});
};
})( jQuery );
@redconfetti
Copy link

Thanks for posting this.

@thiagocordeiro
Copy link

nice job, i've improved a little:

file_field.change(function () {
var name = '';
$.each(file_field.get(0).files, function () {
name += ''+this.name + '; ';
});
name = name.substr(0, (name.length-2));
nice_file_block_element.find("input").val(name);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment