Hey there! I've implemented accordion functionality using the addClass() and removeClass() methods. Here's a breakdown of what I did:
<div class="container">
<div class="functionality">Accordion</div>
<ul class="accordion-wrapper">
<li>
<div class="title">Title</div>
<div class="text">Description</div>
</li>
<li>
<div class="title">Title</div>
<div class="text">Description</div>
</li>
<li>
<div class="title">Title</div>
<div class="text">Description</div>
</li>
</ul>
</div>
I styled it with CSS like this:
ul {
list-style: none;
}
.accordion-wrapper .title {
border: 1px solid #0F0F0F;
background: #CCCCCC;
font-size: 14px;
margin: 0 10px 0 10px;
padding: 6px 8px;
cursor: pointer;
}
.accordion-wrapper .text {
display: none;
font-size: 12px;
padding: 8px 16px;
text-align: justify;
}
.accordion-wrapper .active {
display: block;
}
.functionality {
font-size: 16px;
margin: 12px 8px;
padding: 6px 8px;
}
And here's the JavaScript part:
(function($) {
$(function() {
function accordion() {
var $clicked = $('.title');
var $text = $('.text');
$clicked.click(function() {
var $instance = $(this);
var $currentText = $instance.closest('li').find('.text');
if ($currentText.hasClass('active')) {
$currentText.removeClass('active');
} else {
$text.removeClass('active');
$currentText.addClass('active');
}
});
}
accordion();
});
})(jQuery);
Now, I'm looking for a way to switch from using addClass() and removeClass() to toggleClass(). Any ideas or suggestions on how I can do that without creating a new accordion? Thanks!