javascript - Jquery manipulating number of elements with given pattern of id -
code below hides section if radio button unchecked.
if ($('#items_0__suffix:checked').val() === 'no') { $('#section0').hide(); }
now if have multiple items, succinct way write code rather code below:
if ($('#items_0__suffix:checked').val() === 'no') { $('#section0').hide(); } if ($('#items_1__suffix:checked').val() === 'no') { $('#section0').hide(); }
assuming sequence of checkbox id starts 0 , has corresponding element id starts zero, can use jquery starts with
selector , use jquery index()
figure out element show/hide.
$("[id^=items_]").on("click",function() { var index = $(this).index(); !$(this).is(":checked") ? $('#section' + index).hide() : $('#section' + index).show(); });
example : https://jsfiddle.net/97bo2vyb/2/
Comments
Post a Comment