Pouvez-vous faire un meilleur code? Je dois cocher/décocher tous les enfants en fonction du parent et quand un enfant est coché, cocher parent, quand tous les enfants sont décochés, décochez parent.Cocher/décocher les parents et les enfants
$(".parent").children("input").click(function() {
$(this).parent().siblings("input").attr("checked", this.checked);
});
$(".parent").siblings("input").click(function() {
if (this.checked) {
$(this).siblings("div").children("input").attr("checked", true);
return;
}
var childs = $(this).siblings("div").siblings("input");
for (i = 0; i < childs.length; i++) {
if ($(childs.get(i)).attr("checked"))
return;
}
$(this).parent().children("div").children("input").attr("checked", false);
});
quel est le but de faire ".siblings (...). Siblings (...)"? Aussi: "$ (this) .parent(). Children (...)" pourrait être ".siblings (...)" – nickf
nickf: guh, pour voir si vous faisiez attention :) - en fait, je viens copié à partir du code de Zote (ligne var childs) - je l'ai nettoyé plus (+1) – Hafthor