How to check if a file was selected in the <input type = "file"> element?
I have several checkboxes and an input for uploading files. I would like to turn on the button again if one or more of the checkboxes is marked AND, if the input value is not null.
Here is the link for bootply
Here is my html
<div class="upload-block">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="file" id="InputFile">
<button id="upload-btn" type="button blue-button"
class="btn blue-button" disabled>Submit</button>
</div>
Here is my javascript starting point: Updated via Karl Bind the event changeto all inputs and then use some condition:
$('.upload-block input').change(function() {
$('#upload-btn').prop(
'disabled',
!($('.upload-block :checked').length && $('#InputFile').val()));
});
This works for all checkboxes, but #InputFilehas a value other than none. those. where is the file selected. However this does not work in IE9
How can I update this to work in IE9?
+4
5
Pure JS:
<input type="file" id="InputFile">
<button onclick="buttonSubmitClicked(event)">Submit</button>
<script>
function buttonSubmitClicked(event) {
if (!document.getElementById("InputFile").value) {
event.preventDefault();
alert("Please choose a file!");
} else {
alert("File've been chosen");
}
}
</script>
+4