Saturday, 15 May 2010

jQuery selecting all child checkboxes -



jQuery selecting all child checkboxes -

firstly, wanted hopeless jquery.

i looking @ code post on here selects each parent checkbox (and works):

$(function() { $(":checkbox").change(function () { $(this).parents().children(':checkbox').attr('checked', this.checked); }); });

we have tree view construction using <ul> <li> , each <li> has checkbox. want opposite of above , perform same checkbox selection on kid checkboxes.

can modify above that?

just note using razor , each checkbox @html.checkboxfor output along lines of:

<input name="survey.buildings[0].isselected" type="checkbox" value="true" checked="checked" data-val="true" data-val-required="the selected? field required." id="survey_buildings_0__isselected" /> <input name="survey.buildings[0].isselected" type="hidden" value="false" /> edit 2:

ok have fixed html , construction looks like:

<ul> <li> cbx1 <ul> <li> cbx 2 </li> </ul> </li> <li> cbx3 </li> <li> cbx4 <ul> <li> cbx 5 </li> <li> cbx 6 </li> </ul> </li> </ul>

so if cbx4 checked cbx5 , 6 checked, , if unchecked unchecked

much appreciated

andy

jsfiddle demo

$(function() { $("input[type='checkbox']").change(function () { $(this).siblings('ul') .find("input[type='checkbox']") .prop('checked', this.checked); }); });

jquery

No comments:

Post a Comment