I want to refactor these code, but i don't know how to implement?
Original code:
$('#members_all').click(function(){
if(this.checked){
$(".members").each(function(){
this.checked = true;
});
} else {
$(".members").each(function(){
this.checked = false;
});
}
});
$('#games_all').click(function(){
if(this.checked){
$(".games").each(function(){
this.checked = true;
});
} else {
$(".games").each(function(){
this.checked = false;
});
}
});
My refactor idea:
var arr = ["members","games"];
for(var i=0; i< arr.length;i++){
$('#'+arr[i]+'_all').click(function(){
$("."+arr[i]).each(function(){this.checked =($('#'+arr [i]+'_all').attr("checked") == "checked");});
});
}
but test in browser, I got wrong result.