I’m looking for a solution regarding following issue:
On the current website I’m building, there’s a sub page with an accordion which has a few sections. My goal is to create anchor links to each section of the accordion, that’s not where my problem is. What I want to happen is for the accordion to open the right section when the anchor link is used, even when opened from an external website. Right now the page scrolls down to the accordion itself and just stops there.
Does anyone know how to do what I tried to explain?
Concrete Core Version - 9.2.1
Concrete Version Installed - 9.2.1
<script>
document.addEventListener("DOMContentLoaded", function () {
// Function to open accordion based on URL hash
function openAccordionByHash() {
const hash = window.location.hash; // Get the hash from the URL
if (hash) {
const target = document.querySelector(hash); // Find the element with the ID matching the hash
if (target && target.classList.contains('accordion-collapse')) { // Check if the target is an accordion-collapse
const collapse = new bootstrap.Collapse(target, {
toggle: false // Prevents immediate toggling
});
collapse.show(); // Show the accordion panel
// Optionally, scroll to the opened accordion item
setTimeout(() => target.scrollIntoView({ behavior: 'smooth' }), 300);
}
}
}
// Check if there's a hash on page load and open the corresponding accordion
openAccordionByHash();
// Add event listener to update hash on accordion toggle
const accordionToggles = document.querySelectorAll('.accordion-button');
accordionToggles.forEach(toggle => {
toggle.addEventListener('click', function () {
const collapseId = this.getAttribute('data-bs-target'); // Get the collapse target ID
if (collapseId) {
window.location.hash = collapseId; // Update the URL hash
}
});
});
});
</script>
the links would be
<a href="webpage.html#collapse202">Open Panel 1 on Another Page</a>
<a href="webpage.html#collapse203">Open Panel 2 on Another Page</a>
this is not tested but it gives you the write idea.