I have a 10 items listed on a page, with checkboxes next to each item. I'd like to create a tool that depicts the dependencies between each item.
Example: Item 1 is required for items 2, 3 and 4 to work. Item 3 is required for items 2, 4 and 5 to work. If I click the checkbox next to item 1 alone, I'd like to change the text color of items 1, 2, 3, and 4 from the default green to red (indicating that they don't work now because the prerequisite data item is checked). If I click the checkbox next to item 3 alone, I'd like to change the text color of items 2, 3, 4 and 5 to red but keep item 1 green.
Assume item 1 is already checked, and then I check item 3, I want items 1 through 4 to remain red and have item 5 go red as well. At this point if I was to uncheck item 1, only item 1 should go green as items 2,3,4 and 5 are also dependent on the checkbox for item 3. Alternatively, if I was to uncheck item 3, only item 5 should go green while the rest remain red.
I tried using the toggleClass function in JQuery but it did not give me the desired results. What's the best way to accomplish this with JQuery?
Attempted Toggle Code:
$('#item1').click(function(){
var item = this.checked;
$('#item1, #item2, #item3, #item4').toggleClass('green', !item).toggleClass('red', item);
});
$('#item3').click(function(){
var item = this.checked;
$('#item5, #item2, #item3, #item4').toggleClass('green', !item).toggleClass('red', item);
});
This code worked initially, but when one item was checked already and then another was checked, then removed, it did not display the correct results.