Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a file has been selected in a <input type="file"> element?

I have multiple checkboxes and a file upload input. I would like to re-enable a button if one or more checkbox's are checked AND if the input value is not null.

Here is a link to bootply

Here 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 a change event on all input and then use some condition:

  $('.upload-block input').change(function() {
    $('#upload-btn').prop(
        'disabled',
        !($('.upload-block :checked').length && $('#InputFile').val()));
  });

Example

This works for all the checkboxes and #InputFile has a value other than none. i.e. where a file has been chosen. However this does not work in IE9

How can I update this to work in IE9?

like image 231
nolawi Avatar asked Jul 18 '14 14:07

nolawi


People also ask

How do you check file is selected or not in JS?

length property to check file is selected or not. If element. files. length property returns 0 then the file is not selected otherwise file is selected.

How can you tell if a file is canceled and clicked on input?

The property value type=”file” tells that the type of input the user enters is a file. The property value id=”theFile” is used to link the JavaScript code to it using getElementById() method. The property value onclick=”initialize()” is used to specify the function call when the user has clicked on the input.

How do I get rid of no file chosen from input type file?

Remove the value('No file chosen'). Use . addClass() method to add the class which removes the value “No file chosen”.


1 Answers

Bind a change event on all input and then use some condition:

  $('.upload-block input').change(function() {
    $('#upload-btn').prop(
        'disabled',
        !($('.upload-block :checked').length && $('#InputFile').val()));
  });

Example

like image 119
Karl-André Gagnon Avatar answered Oct 05 '22 22:10

Karl-André Gagnon