Q:

jQuery function in order to check checkboxes

I need some help with jQuery.
I have a forms with checkboxes, e.g.:
<form name="myform">
<input type="checkbox" name="apple[]" value="12" />
<input type="checkbox" name="apple[]" value="12" />
<input type="checkbox" name="apple[]" value="14" />
<input type="checkbox" name="apple[]" value="15" />
<input type="checkbox" name="peach[]" value="12" />
<input type="checkbox" name="peach[]" value="14" />
<input type="checkbox" name="peach[]" value="15" />

Here is the jQuery that has been tried:
$(document).ready(function(){ 
    $(":checkbox").change(function(){ 
        if($('.peach').attr("checked")) { 
           alert("Checked"); 
        } 
        else { 
             alert("Unchecked"); 
        } 
     }); 
 }); 

When I check a checkbox named "peach" I should be able to have checked also checkboxes named "apple" with the same value as "peach". So if I check "peach" with 12 as its value, then also checkboxes named "apple" with 12 as their value should be checked.
I am new to jQuery so I cannot find a solution by myself... would someone help me? :-)
Regards
Federico

A:

Try the following:
$('input[name^="peach"]').change(function() {
    var apple = $('input[name^="apple"][value='+ this.value +']') // phew
    if (this.checked) {
       apple.prop('checked', true)
    } else {
       apple.prop('checked', false)
    }       
})

Demo

