I am seeking assistance in resolving the JavaScript code for my sidebar. While I've noticed that most people use jQuery for this task, I would prefer to stick with plain JavaScript as I am still new to it.
If you are reading this, please refrain from marking it as a duplicate or irrelevant. I have been researching JavaScript for some time now and hope this post will be beneficial not only for me but also for others interested in using regular JavaScript.
Recently, I learned how to create a simple accordion from w3Schools, which you can find here.
Currently, I have successfully designed my custom sidebar. However, I am facing an issue where I want the active accordion to close when I click on another accordion.
Playground:
const sideBar = document.getElementsByClassName('pisti-sidebar');
for (let i = 0; i < sideBar.length; i++) {
sideBar[i].addEventListener('click', function() {
const sideBarTitle = this.nextElementSibling;
sideBarTitle.style.display == 'none';
if (sideBarTitle.style.display === 'block') {
sideBarTitle.style.display = 'none';
} else {
sideBarTitle.style.display = 'block';
document.querySelectorAll('.pisti-sidebar')[i].classList.remove('onload-active');
document.querySelectorAll('.pisti-sidebar-title')[i].classList.remove('onload-active');
}
});
}
body {
background: #000000 !important;
width: 100vw;
height: 100vh;
padding: 20px;
}
/* pisti-sidebar styles */
.pisti-sidebar {
// Add your styles here
}
.onload-active {
// Add your styles here
}
.onload-unactive {
// Add your styles here
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<div class="container">
<div class="row">
<div class="col-12">
<button class="pisti-sidebar onload-active" data-pisti-sidebar="pisti-sidebar-1">Appareal</button>
<div class="pisti-sidebar-title onload-active">
<a href="#" class="pisti-sidebar-content">Custom shirt</a>
<a href="#" class="pisti-sidebar-content">Event shirt</a>
<a href="#" class="pisti-sidebar-content">Corporate shirt</a>
<button class="pisti-sidebar" data-pisti-sidebar="pisti-sidebar-2">Digital Printing</button>
<a href="#" class="pisti-sidebar-content">Poster</a>
<a href="#" class="pisti-sidebar-content">Banner</a>
<a href="#" class="pisti-sidebar-content">Bunting</a>
<button class="pisti-sidebar" data-pisti-sidebar="pisti-sidebar-3">Offset Printing</button>
<a href="#" class="pisti-sidebar-content">Name card</a>
<a href="#" class="pisti-sidebar-content">Flyer</a>
<a href="#" class="pisti-sidebar-content">Booklet</a>
</div>
</div>
</div>
In an attempt to address this, I added the following:
document.querySelectorAll('.pisti-sidebar')[0].classList.remove('onload-active');
document.querySelectorAll('.pisti-sidebar-title')[0].classList.remove('onload-active');
This removed the class from the first accordion, causing it to close. However, I am unsure how to close the second accordion when clicking on the third or first accordion.