Skip to content

Instantly share code, notes, and snippets.

@yaroslavKas
Created September 19, 2016 09:32
Show Gist options
  • Save yaroslavKas/078ac76e3929e7c5d407ef8ff6dc766f to your computer and use it in GitHub Desktop.
Save yaroslavKas/078ac76e3929e7c5d407ef8ff6dc766f to your computer and use it in GitHub Desktop.
Стилизация input "file"
<div class="file_upload">
<button type="button">Выбрать</button>
<div>Файл не выбран</div>
<input type="file">
</div>
var wrapper = $( ".file_upload" ),
inp = wrapper.find( "input" ),
btn = wrapper.find( "button" ),
lbl = wrapper.find( "div" );
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 ] )
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 );
btn.text( "Выбрать" );
}else
btn.text( file_name );
}).change();
$( window ).resize(function(){
$( ".file_upload input" ).triggerHandler( "change" );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment