Home > Mobile >  Checkbox automatic disabled when checked in autopunching/ autocoding
Checkbox automatic disabled when checked in autopunching/ autocoding

Time:12-23

Hi can anybody has better idea than this one? Because this codes needs clicking the box to enable the disabled function. I want an automatic disabled if the box is already checked. Please help me i am just an amateur programmer not like you guys.. Thanks!

$ = jQuery; 
$(document).ready(function(){ 
  $("[type='checkbox']").on('change',function(){ 
    if($(this).is(':checked')) { 
      $("[type='checkbox']").prop("disabled", true)
        .css("background","rgba(135, 206, 235, 0.4)"); 
    } 
  }); 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

I was expecting a auto disable of those box that were not checked.

CodePudding user response:

Simple remove the change event callback so that the if statement executes as soon as the page is ready.

$ = jQuery; 
$(document).ready(function(){ 
    if($("[type='checkbox']").is(':checked')) { 
      $("[type='checkbox']").prop("disabled", true)
        .css("background","rgba(135, 206, 235, 0.4)"); 
    } 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  • Related