So, I have two unordered lists with the same number of elements in them. Therefore, let items in unordered list # 2 be hidden. The only way to do this is if you click on the items in unordered list # 1.
therefore basically
<ul class="list1"> <li>item 1</li> <li>item 2</li> <li>item 3</li> <li>item 4</li> </ul> <ul class="list2"> <li class="hide">item 1</li> <li class="hide">item 2</li> <li class="hide">item 3</li> <li class="hide">item 4</li> </ul>
Now the way I'm trying to do this is to use the index() method, but I'm not sure how to properly approach this code.
This is how I thought about it.
$('.list1').on('click', 'li', function() { $('.list2 li').index($(this).index()).toggleClass('active'); });
so when you click on a line item in .list1 , regardless of the index of that line item, this is the index I want to target in .list2
The problem I am facing is that when I register the console, I get weird indexes. The first position will be displayed as 2, not 0, and the index for the second position will be -1.
what am I doing wrong? I'm sure.
Thanks in advance guys!
source share