Home > Net >  Expanding accordion based on anchor
Expanding accordion based on anchor

Time:11-03

Is there a way to expand Bootstrap accordion based on an URL anchor?

For example if I load the page with the following URL: /mypage#headingOne I'm using PHP but I understand that none of the URL part after # is passed via server. Perhaps this could be done via JS?..

<div class="accordion" id="accordionExample">
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingOne">
      <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
    Accordion Item #1
</button>
    </h2>
    <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the first item's accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingTwo">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
    Accordion Item #2
</button>
    </h2>
    <div id="collapseTwo" class="accordion-collapse collapse" aria-labelledby="headingTwo" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the second item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
</div>

CodePudding user response:

Here is a pure js implement without jQuery live demo. you could place the code below at bottom of your html body.

(function() {
    // change location hash when toggle a accordion item.
    document.querySelectorAll('.accordion-button').forEach(function(btn) {
        btn.addEventListener('click', function() {
            window.location.hash = '#'   this.parentNode.id;
        });
    });
    if (!window.location.hash) return;
    // show accordion item based on location hash.
    let act = document.querySelector(window.location.hash)
    if (act) {
        let btn = act.querySelector('.accordion-button');
        if (btn && !btn.parentNode.parentNode.querySelector('.collapse.show')) {
            btn.click();
        }
    }
})();
  • Related