I have this format.
<div class="container" style="display: flex;">
<div style="width: 25%;">
<div class="collapsible-title is-active">Title 1
<i class="fas fa-solid fa-chevron-right"></i>
</div>
<div class="collapsible-title">Title 2
<i class="fas fa-solid fa-chevron-right"></i>
</div>
</div>
<div style="width: 75%;">
<div class="collapsible-content is-active">
<h3>Title 1 Content</h3>
<p>
Lorem Ipsum is simply dummy text.
</p>
</div>
<div class="collapsible-content">
<h3>Title 2 Content</h3>
<p>
Lorem Ipsum has been the industry's standard dummy text.
</p>
</div>
</div>
</div>
Now when I click on "Title 2" on first div, I want to remove all "is-active" class, and put "is-active" class in "Title 2" form first div and div of "Title 2 content" in second div.
(function ($) {
$(".collapsible-title").on("click", function () {
$(this).addClass("is-active").siblings(".collapsible-title").removeClass("is-active");
$(".collapsible-content").addClass("is-active").siblings(".collapsible-content").removeClass("is-active");
});
});
2
Answers
If the order of the elements are the same then you can add
.eq($(this).index())
to your code.This will add the class to the corresponding div to the clicked element.
Whichever div is clicked, its content is displayed. Are you looking for something like this?