Last active
April 26, 2018 08:33
-
-
Save zdimaz/8b522d3e2ce88cb36abed69be7635806 to your computer and use it in GitHub Desktop.
File input js
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
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