Im using several bootstrap collapse elements on my website. The Collapse elements are controlled with read-more buttons, that have an icon in them that changes depending of the state of the collapse element. I achieve that by adding a class to the button. The problem is that the class is added to all the buttons. How do I add the class to only the specific collapse element button?
Here is the code:
<div class="collapse extra-text" id="collapse1">
<p class="">Hidden text</p>
</div>
<p>
<a class="read-more" data-bs-toggle="collapse" data-bs-target="#collapse1" aria-expanded="false" aria-controls="collapseExample">Read more </a>
</p>
The js im using:
<script>
$('.extra-text').on('hidden.bs.collapse', function () {
$('.read-more').removeClass('active');
});
$('.extra-text').on('shown.bs.collapse', function () {
$('.read-more').addClass('active');
});
</script>
And the css:
a.read-more:after {
font-family: "Font Awesome 5 Free";
content: "\f078";
display: inline-block;
transition: all 0.5s;
}
a.read-more.active:after {
-webkit-transform: rotate(180deg);
-moz-transform: rotate(180deg);
transform: rotate(180deg);
}