I have a list of items. The first item automatically has the class "active". When clicking another item in this list the class "active" is removed from the first item and added to the clicked item in the list. So far, so good.
But, I want another div to add/remove a class based on the clicked item in the list.
For example
List Item 1 has class "active" > Div Item 1 also has class "active"
Clicking on list item 3: List Item 1 class "active" is removed and added to list item 3 > Div Item 1 class "active" is removed and added to div 3
So far I have this:
$('.case-1').addClass('active-case');
$('#case-1').addClass('active');
$("#carousel-nav ul li").click(function(){
$('#carousel-nav ul li.active').not(this).removeClass('active');
$(this).toggleClass('active');
})
But I can't find a way to achieve the addClass matching list item and div.
HTML:
<div id="carousel-nav">
<ul>
<li id="case-1"><a href="#"></a></li>
<li id="case-2"><a href="#"></a></li>
<li id="case-3"><a href="#"></a></li>
<li id="case-4"><a href="#"></a></li>
<li id="case-5"><a href="#"></a></li>
<li id="case-6"><a href="#"></a></li>
<li id="case-7"><a href="#"></a></li>
</ul>
</div>
<div id="case-wrapper" class="case-1> /* Post */ </div>
<div id="case-wrapper" class="case-2> /* Post */ </div>
<div id="case-wrapper" class="case-3> /* Post */ </div>
<div id="case-wrapper" class="case-4> /* Post */ </div>
<div id="case-wrapper" class="case-5> /* Post */ </div>
<div id="case-wrapper" class="case-6> /* Post */ </div>
<div id="case-wrapper" class="case-7> /* Post */ </div>
NEVER assing the same id more than once in the same document. I would suggest to change the markup as shown below:
Use
data-numberattributes to match a list item with it's respective div element.jQuery