Skip to content

Instantly share code, notes, and snippets.

@zdimaz
Last active April 26, 2018 08:33
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 zdimaz/8b522d3e2ce88cb36abed69be7635806 to your computer and use it in GitHub Desktop.
Save zdimaz/8b522d3e2ce88cb36abed69be7635806 to your computer and use it in GitHub Desktop.
File input js
div.input_file_box
a(href="javascript:;").files_btn
| Прикрепить документ
span.name_file
a(href="javascript:;" class="clear_file")
input(type="file" class="hide")
self.fileInput();
/**
** File input
**/
fileInput: function(){
$('.files_btn').on('click', function(){
var $this = $(this),
input = $this.parent().find('input[type="file"]');
input.trigger('click');
});
$('input[type="file"]').on('change',function(){
var $this = $(this),
nameFileBox = $this.parent().find('.name_file'),
nameFile = $this.val();
if ($(this).val().lastIndexOf('\\')) {
var n = $(this).val().lastIndexOf('\\') + 1;
} else {
var n = $(this).val().lastIndexOf('/') + 1;
}
var nameFile = $(this).val().slice(n);
if(nameFile != ''){
nameFileBox.text(nameFile).css('display', 'block');
$this.parent().addClass('selected');
}
else{
nameFileBox.text(nameFile).css('display', 'none');
$this.parent().removeClass('selected');
}
});
$('.clear_file').on('click', function(){
var $this = $(this),
nameFileBox = $this.parent().find('.name_file');
$this.parent().removeClass('selected').find('input').val('');
nameFileBox.text('').css('display', 'none');
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment