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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -