Jake Jake - 10 months ago 38
jQuery Question

JQuery - checkbox enable/disable

I have a bunch of checkboxes like this. If the "Check Me" checkbox is checked, all the other 3 checkboxes should be enabled, else they should be disabled. How can I do this using Jquery?

<form name="frmChkForm" id="frmChkForm">
<input type="checkbox" name="chkcc9">Check Me
<input type="checkbox" name="chk9[120]">
<input type="checkbox" name="chk9[140]">
<input type="checkbox" name="chk9[150]">


Change your markup slightly:

<form name="frmChkForm" id="frmChkForm">
<input type="checkbox" name="chkcc9" id="group1">Check Me
<input type="checkbox" name="chk9[120]" class="group1">
<input type="checkbox" name="chk9[140]" class="group1">
<input type="checkbox" name="chk9[150]" class="group1">

and then

$(function() {

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

You can do this using attribute selectors without introducing the ID and classes but it's slower and (imho) harder to read.