Reputation: 61
I have a form with two sets of checkboxes. The first is a list of roles on a committee (chair, vice chair, secretary, etc.) The second set of checkboxes is a list of people in those roles. For instance, Todd is our vice chair and Gwen is our treasurer. If I want to have Todd's checkbox selected when I click the vice-chair checkbox, I can do this:
$('input[name=toddS]').attr('checked', true);
But if I want to select our treasuer, I want it to not only check Gwen's checkbox, but uncheck Todd's (and all other selected checkboxes. How do I do this so that no matter what people check, it leaves only the person or people in that role checked and leaves all of the remaining checkboxes unchecked. Is that clear? Hope so.
Scott
Upvotes: 3
Views: 2047
Reputation: 1628
If the list of names looks like this
<input type="checkbox" name="person" value="Todd" />Todd
<input type="checkbox" name="person" value="Gwen" />Gwen
<input type="checkbox" name="person" value="Micheal" />Micheal
you can use the following js to clear all selections
$('input[name=person]').attr('checked', false);
A bit of a late answer to help you perhaps but may help someone else...
Upvotes: 0
Reputation: 382616
Use radio buttons instead, this way only one radio button will be checked and others un-checked automatically. No need for even jQuery. Just make sure that you name the radio buttons the same with different value, eg:
<input type="radio" name="position" value="Chairman" /> Chairman
<input type="radio" name="position" value="Vice Chairman" />Vice Chairman
<input type="radio" name="position" value="Secretory" /> Secretory
<!-- and so on -->
Similarly, for people's:
<input type="radio" name="person" value="Todd" /> Todd
<input type="radio" name="person" value="Gwen" />Vice Gwen
<input type="radio" name="person" value="Micheal" /> Micheal
<!-- and so on -->
Upvotes: 1