Based on the provided code, I see that your implementation is nearly correct, but there are a few issues that might affect the behavior of your accordions, particularly how they update their icons and the coexistence of the two accordions.
Here are the necessary adjustments and improvements, along with explanations to ensure the accordions behave as you intended:
Problems Identified
- Duplicate IDs: Both accordions use the same IDs for their headers and collapse sections. This can lead to unpredictable behavior since HTML IDs must be unique within a document.
- Use of
data-bs-parent: Your first accordion's data-bs-parent attribute is pointing to an incorrect ID (#benefitsAccordion instead of #benefitAccordion).
- Icon Toggle Logic: The icon toggle function currently resets icons based on the class of the button, but it may not handle the collapsing state correctly, especially if the accompanying collapse structure does not reflect the proper states.
Revised Implementation
Here’s a corrected version of your provided code that addresses these issues:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css" rel="stylesheet">
<title>Accordions Example</title>
</head>
<body>
<div class="container mt-5">
<!-- First Accordion: Benefit Accordion -->
<div id="benefitAccordion" class="accordion accordion-flush">
<!-- Benefits #1 -->
<div class="accordion-item">
<h2 class="accordion-header" id="headingBenefitOne">
<button class="accordion-button fw-bold" type="button" data-bs-toggle="collapse" data-bs-target="#collapseBenefitOne"
aria-expanded="true" aria-controls="collapseBenefitOne" data-bs-parent="#benefitAccordion">
Tailored for Small Businesses
<i class="bi bi-box-arrow-in-up fw-bold ms-auto" aria-hidden="true" style="font-size: 1.4rem;"></i>
</button>
</h2>
<div id="collapseBenefitOne" class="accordion-collapse collapse show" aria-labelledby="headingBenefitOne">
<div class="accordion-body">
Your success is our priority. We understand that small businesses face unique challenges, whether it’s establishing a brand identity or managing contacts. Our tailored features, including customizable virtual card templates, unique QR codes, and smart prompts for contact follow-ups, are designed to support your growth and help you connect effectively.
</div>
</div>
</div>
<!-- Benefits #2 -->
<div class="accordion-item">
<h2 class="accordion-header" id="headingBenefitTwo">
<button class="accordion-button collapsed fw-bold" type="button" data-bs-toggle="collapse" data-bs-target="#collapseBenefitTwo"
aria-expanded="false" aria-controls="collapseBenefitTwo" data-bs-parent="#benefitAccordion">
We Value Feedback
<i class="bi bi-box-arrow-down fw-bold ms-auto" aria-hidden="true" style="font-size: 1.4rem;"></i>
</button>
</h2>
<div id="collapseBenefitTwo" class="accordion-collapse collapse" aria-labelledby="headingBenefitTwo">
<div class="accordion-body">
Your opinions shape our growth. Through surveys, user testing, and direct feedback channels, we actively listen to our users to enhance our offerings. New features and updates come directly from your suggestions, ensuring that TheCard.com evolves according to your needs.
</div>
</div>
</div>
</div>
<!-- Second Accordion: FAQ Accordion -->
<div id="faqAccordion" class="accordion accordion-flush">
<!-- Question 1 -->
<div class="accordion-item">
<h2 id="headingFAQOne" class="accordion-header">
<button class="accordion-button fw-bold" type="button" data-bs-toggle="collapse" data-bs-target="#collapseFAQOne"
aria-expanded="true" aria-controls="collapseFAQOne">
Why is your service free at this time?
<i class="bi bi-box-arrow-in-up fw-bold ms-auto" aria-hidden="true" style="font-size: 1.4rem;"></i>
</button>
</h2>
<div id="collapseFAQOne" class="accordion-collapse collapse show" aria-labelledby="headingFAQOne">
<div class="accordion-body">
We are currently in beta, and we want to gather valuable feedback from users like you. Offering our service for free during this phase allows us to improve our platform based on real user experiences.
</div>
</div>
</div>
<!-- Question 2 -->
<div class="accordion-item">
<h2 id="headingFAQTwo" class="accordion-header">
<button class="accordion-button collapsed fw-bold" type="button" data-bs-toggle="collapse" data-bs-target="#collapseFAQTwo"
aria-expanded="false" aria-controls="collapseFAQTwo">
How long will the beta phase last?
<i class="bi bi-box-arrow-down fw-bold ms-auto" aria-hidden="true" style="font-size: 1.4rem;"></i>
</button>
</h2>
<div id="collapseFAQTwo" class="accordion-collapse collapse" aria-labelledby="headingFAQTwo">
<div class="accordion-body">
While we don’t have an exact timeline, we aim to collect feedback and make necessary improvements over the next while. We will keep our users updated on our progress and any changes we implement.
</div>
</div>
</div>
</div>
</div>
<script>
document.addEventListener("DOMContentLoaded", () => {
const toggleIcons = (accordionId) => {
const accordionButtons = document.querySelectorAll(`${accordionId} .accordion-button`);
accordionButtons.forEach(button => {
button.addEventListener('click', () => {
const icon = button.querySelector('i');
// Toggle the icon for the clicked button
if (icon) {
icon.classList.toggle('bi-box-arrow-down');
icon.classList.toggle('bi-box-arrow-in-up');
}
// Reset icons for other buttons in the same accordion
accordionButtons.forEach(otherButton => {
if (otherButton !== button) {
const otherIcon = otherButton.querySelector('i');
if (otherIcon) {
const isCollapsed = otherButton.classList.contains('collapsed');
otherIcon.classList.toggle('bi-box-arrow-down', isCollapsed);
otherIcon.classList.toggle('bi-box-arrow-in-up', !isCollapsed);
}
}
});
});
});
};
// Initialize accordions
toggleIcons('#benefitAccordion');
toggleIcons('#faqAccordion');
});
</script>
</body>
</html>
Key Changes Made:
-
Unique IDs: Each element that utilizes id attributes has been given unique identifiers, such as headingBenefitOne, collapseBenefitOne, headingFAQOne, etc. This ensures there are no conflicts when accessing those elements.
-
Correct Use of data-bs-parent: The data-bs-parent attribute on the buttons within the benefit accordion points to #benefitAccordion, ensuring that only one item opens at a time.
-
JavaScript Logic: The provided script manages icon toggling properly for both accordions without interfering with each other.
Expected Behavior:
- Benefit Accordion: Only one item can be opened at a time. When you open a new item, any other open item closes automatically.
- FAQ Accordion: Multiple items can be opened independently. Opening or closing one item does not affect the state of others in the same accordion.
This structure ensures that each accordion functions according to its intended behavior, while the JavaScript appropriately manages the icon toggling for both accordions. If you follow this implementation, you should not see any unintended interactions between the two accordions.