Created
March 29, 2019 06:31
-
-
Save micti/bca582bc4054ca7b034faea56930221c to your computer and use it in GitHub Desktop.
Display name of file for Bulma Input File
This file contains hidden or 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
<!-- 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> |
This file contains hidden or 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
// 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' | |
} | |
}) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have this for displaying the name (delete and default name I set it via template):