views:

55

answers:

1

I have this:

$('input.people').attr('checked', function() {
  return $.inArray(this.name, ['danB', 'lindaC']) != -1;
});

It is for a list of contacts. I want to be able to have the user click a checkbox that will select the people in a specific role and uncheck everyone else. That works. Now I want to modify this so that if someone unclicks the same checkbox that it deselects those people (and only those people). Would that be an onBlur event?

I'm thinking something like this:

$('input.people').attr('checked', function(){
   return $.inArray(this.name, ['danB', 'lindaC']) != 1;
});

or would it be:

$('input.people').attr('checked', function(){
   return $.inArray(this.name, ['danB', 'lindaC']) = -1;
});

And how would I integrate this with the top function? A shout out, btw, to Nick Craver for his help to date.

A: 
$('input.people').change(function ()
{
    if(!$(this).hasClass("checked"))
    {
        //do stuff if the checkbox is checked
        $(this).addClass("checked");
        return;
    }

    //do stuff if the checkbox isn't checked
    $(this).removeClass('checked');
});
Moin Zaman
see here for a discussion on why this works and what the shortcomings of other approaches are: http://stackoverflow.com/questions/355638/jquery-toggle-event-is-messing-with-checkbox-value
Moin Zaman