Disable checkbox unchecked javascript

2

I need to disable all checkboxes that are not selected / checked. I tried with the following code:

$('input[type=checkbox]').change(function(){
   if($(this).prop("checked", false)){
      $('input[type="checkbox"]:checked', false).prop("disabled", true);   
   }
});

But it's wrong because it's not working.

I need a force to get this to work.

Thank you in advance.

    
asked by anonymous 07.11.2017 / 15:02

1 answer

4

You can do it like this:

$('input[type=checkbox]').each(function() {
  if (!this.checked) this.disabled = true;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="checkbox" checked />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" checked />

Or on one line:

$('input[type=checkbox]:not(:checked)').prop('disabled', true);

The .change() will run only when the value changes, and only on the one that changes.

    
07.11.2017 / 15:04