2

I have some checkBoxes and as long as one of them is ticked, the submit button should be enabled.

However, I do not get the Javascript function right.

In the beginning, the submit button is always disabled, when I tick on checkBox it gets enabled and remains enabled for all the time.

Here is my code:

<td><input  type="checkbox" name="searchValues" th:value="${user.id}" th:checked="${user.isActive}" th:onclick="${'inputClick('''+ itemStat.index +''')'}" /></td>

The Javascript function

<script>    
    $(function(){
        $("input[type='checkBox']").change(function(){
            $("input[type='submit']").prop("disabled", false);
        });
    });
</script>

The submit button

<input type="submit" id="submitButton" th:value="Speichern" name="submit" disabled="disabled"/>

It could be that when loading the site that some checkBoxes are already ticked, so the submit button should be enabled, too. Only when nothing is ticked, the submit button should be disabled.

Maybe someone knows what I am missing here?

Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
Bernd
  • 593
  • 2
  • 8
  • 31

3 Answers3

6

You can check the length of all the checked check boxes. If the length is 0 then you can set the disabled property to true otherwise remove the attribute. You can trigger the change event to set the button property on page load.

$(function(){
  $("input[type='checkBox']").change(function(){
    var len = $("input[type='checkBox']:checked").length;
    if(len == 0)
      $("input[type='submit']").prop("disabled", true);
    else
      $("input[type='submit']").removeAttr("disabled");
  });
  $("input[type='checkBox']").trigger('change');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td><input  type="checkbox" name="searchValues" checked /></td>
    <td><input  type="checkbox" name="searchValues" /></td>
    <td><input  type="checkbox" name="searchValues" /></td>
  </tr>
</table>


<input type="submit" id="submitButton" th:value="Speichern" name="submit" disabled="disabled"/>
Mamun
  • 66,969
  • 9
  • 47
  • 59
  • 1
    You seem to have forgotten to answer the question; how, for example, does your code work? How is the OP - or any other visitor - supposed to learn anything from this code? – David Thomas Dec 24 '18 at 13:21
  • @DavidThomas, Thank you for the advice.....updated the answer with little bit of explanation...... – Mamun Dec 24 '18 at 13:30
  • How can we improve this answer to work on certain ID for instance: I have checkbox with 3 different ID how can I make sure at least one checkbox is check for each ID Many thanks – amprie286 Jun 25 '20 at 09:40
3

Using vanilla Javascript, this would do the job:

// get a list of the checkboxes and spread it into an array
// so later you can use Array methods on it
const checkboxes = [...document.querySelectorAll('input[type=checkbox]')]

// function that tells you if any checkboxes in
// the above list are checked
function anyChecked() {
  // this is where we're using an array method
  // Array.prototype.some
  return checkboxes.some(x=>x.checked)
}

// to every single checkbox, add a click listener
// again, using an Array method
// Array.prototype.forEach
checkboxes.forEach(checkbox => {
  checkbox.addEventListener('click', () => {
    // when any checkbox is clicked,
    // check if there's any checked checkboxes
    anyChecked()
      // if so, enable the button
      ? submitbtn.removeAttribute('disabled')
      // otherwise, disable it
      : submitbtn.setAttribute('disabled','')
  })
})

// do the same thing initially as well 
// to account for initially checked checkboxes
anyChecked()
      ? submitbtn.removeAttribute('disabled')
      : submitbtn.setAttribute('disabled','')
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" checked />
<input type="checkbox" />
<input type="checkbox" />
<button type="submit" id="submitbtn">Submit</button>
connexo
  • 53,704
  • 14
  • 91
  • 128
2

You need to use $(this).is(':checked') in the prop() function to actually check if the checkbox was checked/unchecked instead of just change.

In your case, you need to use !$(this).is(':checked') so that when the checkbox is checked the submit button is enabled and vice-versa.

$(function() {
  $("input[type='checkBox']").change(function() {
    $("input[type='submit']").prop("disabled", !$(this).is(':checked'));
  });
  //for initial case
  $("input[type='checkBox']").trigger('change');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td><input type="checkbox" name="searchValues" th:value="${user.id}" th:checked="${user.isActive}" th:onclick="${'inputClick('''+ itemStat.index +''')'}" checked/></td>
  </tr>
</table>
<input type="submit" id="submitButton" th:value="Speichern" name="submit" disabled="disabled"/>
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
  • 1
    Thank you very much! This was it that I was missing! I will accept your answer in a few minutes, currently I am not allowerd. – Bernd Dec 24 '18 at 13:11
  • I saw soemthing. In the beginning a checkbox could already be ticked, so the button must also be enabled but currently it is not. – Bernd Dec 24 '18 at 13:13
  • @user3003944 does that help now – Ankit Agarwal Dec 24 '18 at 13:20
  • 1
    No, I am sorry, it is still disabled although a checkbox is ticked in the beginning, but @Mamun provided me a fully working example, too. So, thank you very much for your help, too!!! – Bernd Dec 24 '18 at 13:20