Add '#items' in a loop

Viewed 76

Im trying to add the following code in java script into a loop.

 $('#items').find('input').prop('disabled', this.checked);

i want to change the 'items' variable as '#items1', '#items2', '#items3' and so on as a loop.

This code has already worked in this part of my code

var checkList = document.getElementById('list1');

when added to loop to get 'list1', 'list2', 'list3'

var checkList = document.getElementById('list'+list[i].value); 

but it doesnt work for the 'items' in previous code may be because there is a # in front. Please help me with it.

3 Answers

Try this:

var checkList = document.getElementById('list' + (i + 1)); 

Or for best use, try using class instead of id, give all item class called for example: itemClass, then you can get all items:

var checkLists = document.getElementsByClassName('itemClass'); 

the problem is once you using the following codes:

$('#items').find('input')

you got not only one DOM elements. So, you need to using jQuery.each after finding all 'input' elements, and then do what you want.

Thank you Guyss I myself solved the problem. I first got the variable value to a another variable and put it in the function.

var itemId = list[i].value;

I put this inside the function.

$(function() {
   $(document).on('change', '#IsAssociation', function() {                        
   $('#items'+itemId).find('input').prop('disabled', !this.checked);
   $('#items'+itemId).find('li').toggleClass('disabled', !this.checked);
});
Related