12

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?

nolawi
  • 4,439
  • 6
  • 22
  • 45
  • 2
    Think it through, then. How do you get the value of an input using jQuery? Really quick search turns up `val()`. So, now you can get the value, how do you check the length of it? Really quick search turns up the `length` property. How do you add a second condition to the if statements that are already there? A quick search turns up `&&` and `||`. This is the basic learning process. Did you try? – Chris Baker Jul 18 '14 at 15:03
  • thanks chris i knew how to check the value i just wanted to combine it properly and i think its a good question but everytime i ask a question related to jquery I get dinged negatively because the question is easy.. – nolawi Jul 18 '14 at 17:38
  • @nolawipetros, StackOverflow has had many low quality questions come through within the last year. As a result, they are more strict than they used to be. I think it will pass though. A lot of people are working on making things better. – 700 Software Jul 18 '14 at 18:51
  • @Chris and George thanks I did try and made it work via the OR operator but making it work is not the most efficient as there are many ways to do simple things in js and jquery. I spend a lot of on the CSS/Bootrap tags answering q's and it just seems to me that the jquery/js group seems to be the harshest. Anyways thanks for your inputs! – nolawi Jul 18 '14 at 18:58

5 Answers5

13

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

Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
7

Like this:

if($('#InputFile').val().length){
//do your stuff
}

You can better use by using $.trim() method not to allow (trim) spaces:

if($.trim($('#InputFile').val()).length){
//do your stuff
}
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
  • This is simpler `$('#InputFile').val().trim().length` –  Dec 18 '19 at 13:20
  • @Samad `.trim()` is a core JavaScript method whereas `$.trim()` is a jQuery method. – Bhojendra Rauniyar Dec 18 '19 at 18:05
  • Thanks , but jQuery also use JavaScript core methods to do it , and both have same results _____________ `return text==null?"":(text+"").replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"");` –  Dec 21 '19 at 11:22
6

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>

jsfiddle: check it out

Andrew
  • 36,676
  • 11
  • 141
  • 113
2
  $('.upload-block input').change(function() {
        if ($('.upload-block :checked').length && $('#InputFile').val() ) {

          $('#upload-btn').prop('disabled',false);
        }
        else {
          $('#upload-btn').prop('disabled',true); 
        }
         });

No need of separate selectors for checkbox and input file .upload-block input is enough to catch catch both the checkbox and file value change

santhosh
  • 53
  • 1
  • 2
  • 11
1

I would apply the validation to all elements involved. This way changing either the checkboxes or the input will update disabled

$('#InputFile, .upload-block :checkbox').change(function(){
   var isValid= $('.upload-block :checkbox:checked').length  && $('#InputFile').val();
   $('#upload-btn').prop('disabled',isValid);
});
charlietfl
  • 170,828
  • 13
  • 121
  • 150