Skip to content

Instantly share code, notes, and snippets.

@micti
Created March 29, 2019 06:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micti/bca582bc4054ca7b034faea56930221c to your computer and use it in GitHub Desktop.
Save micti/bca582bc4054ca7b034faea56930221c to your computer and use it in GitHub Desktop.
Display name of file for Bulma Input File
<!-- https://bulma.io/documentation/form/file/ -->
<div class="file has-name">
<label class="file-label">
<input class="file-input" type="file" name="resume">
<span class="file-cta">
<span class="file-icon">
<i class="fas fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
Screen Shot 2017-07-29 at 15.54.25.png
</span>
</label>
</div>
// 1. Display filename when select file
// 2. Clear filename when form reset
document.addEventListener('DOMContentLoaded', () => {
// 1. Display file name when select file
let fileInputs = document.querySelectorAll('.file.has-name')
for (let fileInput of fileInputs) {
let input = fileInput.querySelector('.file-input')
let name = fileInput.querySelector('.file-name')
input.addEventListener('change', () => {
let files = input.files
if (files.length === 0) {
name.innerText = 'No file selected'
} else {
name.innerText = files[0].name
}
})
}
// 2. Remove file name when form reset
let forms = document.getElementsByTagName('form')
for (let form of forms) {
form.addEventListener('reset', () => {
console.log('a')
let names = form.querySelectorAll('.file-name')
for (let name of names) {
name.innerText = 'No file selected'
}
})
}
})
@juanifioren
Copy link

I have this for displaying the name (delete and default name I set it via template):

$(function() {
        $("#{{ field.id_for_label }}").change(function() {
            var filename = $(this).val().split('\\').pop();
            $(this).parent().find(".file-name").html(filename);
        });
});

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