Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable and uncheck checkboxes on check of checkbox?

I have a jquery script that disables all checkboxes in a row and marks them as checked:

$(function() {
  enable_cb();
  $("#group1").click(enable_cb);
});

function enable_cb() {
  if (this.checked) {
     $("input.group1").attr("disabled", true);
     $("input.group1").attr("checked", true);
  } else {
     $("input.group1").removeAttr("disabled","checked");

  }
}

Here is my fiddle with the html: http://jsfiddle.net/3fksv/16/

I want the rest of the checkboxes to show active again, and unchecked on uncheck of the checkbox. What am I doing wrong in my script?

like image 939
codeChris Avatar asked Dec 06 '25 17:12

codeChris


1 Answers

checked and disabled are properties, as of jQuery 1.6 for modifying properties of the elements, prop method should be used instead of attr.

function enable_cb() {
   $("input.group1")
         .prop("disabled", this.checked)
         .prop("checked", this.checked);
}

http://jsfiddle.net/JeqE5/

like image 63
undefined Avatar answered Dec 08 '25 11:12

undefined



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!