Hey Mike,

It's perfectly fine to set the checked DOM property there, which takes a Boolean. I think something else is wrong with the OP's code.

I put together a little demo:
http://test.learningjquery.com/checkbox.html
Click the parent checkbox, and all the children checkboxes will toggle between checked and unchecked to match the state of the parent. Here is the code I used:

    $(document).ready(function() {
      $('#city-id-120').click(function() {
        $('.child-checkboxes').attr('checked', this.checked);
      });
    });



--Karl

____________
Karl Swedberg
www.englishrules.com
www.learningjquery.com




On Sep 26, 2009, at 9:52 AM, Mike Alsup wrote:


Please consider the code below, supposed to check all child checkboxes
wjen a parent checkbox has been checked.

//parentCheckbox and childCheckboxes are two variables set earlier.
//Here are typical values they produce once alert()
//I reproduce the exact string outputed when alert() :
//childCheckboxes -> child-city-id-120
//parentCheckbox-> #city-id-120

$('INPUT[class=childCheckboxes]').attr('checked', $ (parentCheckbox).is
(':checked'));

Do you spot the problem?

You want to add or remove the 'checked' attribute, not set its value
to a boolean.  Try something like this:

var $inputs = $('input.childCheckboxes');
if ($(parentCheckbox).is(':checked'))
   $inputs.attr('checked','checked');
else
   $inputs.removeAttr('checked');


Reply via email to