Skip to content

Instantly share code, notes, and snippets.

@98o718
Last active March 16, 2018 13:11
Show Gist options
  • Select an option

  • Save 98o718/659dd814716ac7d1165bc04cc5a7853f to your computer and use it in GitHub Desktop.

Select an option

Save 98o718/659dd814716ac7d1165bc04cc5a7853f to your computer and use it in GitHub Desktop.
File input button value
// File input button value
// HTML
<input type="file" multiple class="input input--hidden input--add-images" id="add-images" accept="image/*">
<label class="input input--label input--label-for-add-images input--dark" for="add-images">Выберете файл</label>
// JS
function fileInputValue(input, label){
var inp = $( input ),
lbl = $( label );
var file_api = ( window.File && window.FileReader && window.FileList && window.Blob ) ? true : false;
inp.change(function(){
var file_name;
if( file_api && inp[ 0 ].files[ 0 ] )
if( inp[0].files.length > 1 )
file_name = inp[0].files.length.toString() + ' файлов';
else
file_name = inp[ 0 ].files[ 0 ].name;
else
file_name = inp.val().replace( "C:\\fakepath\\", '' );
if( ! file_name.length )
return;
if( lbl.is( ":visible" ) ){
lbl.text( file_name );
}
}).change();
};
// Usage
fileInputValue('.input--add-images', '.input--label-for-add-images');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment