I know that styling a file input is pretty minimal, which is not a bad thing.
Is there some way of selecting a file input which is empty?
The plan is to show or hide a submit button depending on whether the file input is empty.
If you are comfortable marking the input as required, you could do it with just css:
input:invalid ~ .chosen {
display: none;
}
input:valid ~ .empty {
display: none;
}
<input type="file" required><br>
<span class="empty">Empty</span>
<span class="chosen">Chosen</span>
Is there some way of selecting file input which is empty?
No.
You'll need JavaScript. You can find several answers here on SO and elsewhere. The basic idea is to watch the element for change events, add a class such as non-empty indicating there are files selected, then style input[type="file"].non-empty + label, assuming you are using the technique of using a label as the replacement file input button. You will have to special-case form.reset() which empties file inputs inside the form but does not trigger a change event on the input. Here's some sample code (without the form.reset part):
// Add a class to a file input if it's not empty.
function mark() { this.classList.toggle('non-empty', this.files.length); }
// Watch a file input and update non-empty status.
function watch(input) { input.addEventListener('change', mark); }
// Get all file inputs.
const inputs = Array.from(document.querySelectorAll('input[type=file]'));
// Watch all file inputs in the whole document.
inputs . forEach(watch);
input[type="file"] { width: 0.1px; height: 0.1px; }
input[type="file"].non-empty + label { color: green; }
input[type="file"].non-empty + label::after { content: ' (specified)'; }
<input type="file" id="input">
<label for="input">Specify file</label>
element.filesyou can't