skip to Main Content

I have an html page with accordion sections with buttons btn-acc-tab. The sections acc-tab open when a user clicks on a button, and each section contains links tab-link that can be active. The sections open and close using the JQuery scripts shown below. I’m trying to have a section acc-tab collapse with a currently active link open when a page loads by adding a class show, not waiting for a user to click. Most answers I could find here deal with activating a show class on click or some other event. Simply substituting event for onload hasn’t worked for me. I know I am missing something simple, but I’ve banged my head on the wall long enough to know that I am not going to see it on my own. Your help will be greatly appreciated.

HTML code:

<div class="container">
    <div class="accordion" id="accordion_Foo">
        <div class="card">
            <div class="card-header" id="heading_Foo_Bar">
                <button class="btn btn-acc-tab" type="button" id="button_Foo_Bar" data-toggle="collapse" data-target="#collapse_Foo_Bar" aria-expanded="true" aria-controls="collapse_Foo_Bar">
                    <h4>Foo Bar</h4>
                </button>
            </div>
            <div class="acc-tab collapse show" id="collapse_Foo_Bar" aria-labeledby="heading_Foo_Bar" data-parent="#accordion_Foo">
                <ul class="tab-list">
                    <li class="btn tab-link active" data-target="Cat" data-id="button_Foo_Bar" tabindex="0" role="button" aria-expanded="true">
                        <a class="active" href="/Foo/Foo_Bar/Cat" target="_self">Cat</a>
                    </li>
                    <span class="sr-only">(current)</span>
                    <li class="btn tab-link" data-target="Dog" data-id="button_Foo_Bar" tabindex="0" role="button" aria-expanded="true">
                        <a class="" href="/Foo/Foo_Bar/Dog" target="_self">Dog</a>
                    </li>
                    <li class="btn tab-link" data-target="Mouse" data-id="button_Foo_Bar" tabindex="0" role="button" aria-expanded="true">
                        <a class="" href="/Foo/Foo_Bar/Mouse" target="_self">Mouse</a>
                    </li>
                </ul>
            </div>
        </div>
        <div class="card">
            <div class="card-header" id="heading_Foo_Foo">
                <button class="btn btn-acc-tab" type="button" id="button_Foo_Bar" data-toggle="collapse" data-target="#collapse_Foo_Foo" aria-expanded="true" aria-controls="collapse_Foo_Foo">
                    <h4>Foo Foo</h4>
                </button>
            </div>
            <div class="acc-tab collapse" id="collapse_Foo_Foo" aria-labeledby="heading_Foo_Foo" data-parent="#accordion_Foo">
                <ul class="tab-list">
                    <li class="btn tab-link active" data-target="Bird" data-id="button_Foo_Foo" tabindex="0" role="button" aria-expanded="true">
                        <a class="active" href="/Foo/Foo_Foo/Bird" target="_self">Bird</a>
                    </li>
                    <span class="sr-only">(current)</span>
                    <li class="btn tab-link" data-target="Fish" data-id="button_Foo_Foo" tabindex="0" role="button" aria-expanded="true">
                        <a class="" href="/Foo/Foo_Foo/Fish" target="_self">Fish</a>
                    </li>
                    <li class="btn tab-link" data-target="Dinosaur" data-id="button_Foo_Foo" tabindex="0" role="button" aria-expanded="true">
                        <a class="" href="/Foo/Foo_Foo/Dinosaur" target="_self">Dinosaur</a>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>

jQuery scripts:

(function ($) {
    $.fn.replaceClass = function (pFromClass, pToClass) {
        return this.removeClass(pFromClass).addClass(pToClass);
    };
}(jQuery));
function clickAndPress(event) {
    if(event.type === 'click'){
        return true;
    }
    else if(event.type === 'keypress'){
        var code = event.charCode || event.keyCode;
        if((code === 32)|| (code === 13)){
            return true;
        }
    }
    else{
        return false;
    }
};

$(".tab-link").on("click keypress", function(){
    if(clickAndPress(event) === true) {
        var id = $(this).data('target')
        var par_id = $(this).data('id');
        $(".tab-content").replaceClass("d-block", "d-none");
        $(".sr-only").remove();
        $(document.getElementById(id)).replaceClass("d-none", "d-block");
        $(".tab-link").removeClass("active");
        if ($(".btn-acc-tab").hasClass("active")) {
            $(".btn-acc-tab").removeClass("active");
        };
        $(this).addClass("active");
        $(document.getElementById(par_id)).addClass("active");
        $('<span class="sr-only">(current)</span>').insertAfter(".active");
    }
});
$(document).ready(function(){
    var active = $(".tab-link.active").data('target');
    $(".acc-tab").addClass("show");
});

Also tried jQuery:

$(this).closest(".btn-acc-tab").addClass("active");

3

Answers


  1. If I understand your problem correctly, what you want is to add show class to acc-tab collapse element if it at least has one element <li> which has active class on page load right? If so, I think you can just use this code inside the $(document).ready(function(){}) function:

        $(".acc-tab").each(function() {
          if ($(this).find('li.active').length > 0) {
            $(this).addClass("show");
          }
        });
    

    Full code:

    (function ($) {
        $.fn.replaceClass = function (pFromClass, pToClass) {
            return this.removeClass(pFromClass).addClass(pToClass);
        };
    }(jQuery));
    function clickAndPress(event) {
        if(event.type === 'click'){
            return true;
        }
        else if(event.type === 'keypress'){
            var code = event.charCode || event.keyCode;
            if((code === 32)|| (code === 13)){
                return true;
            }
        }
        else{
            return false;
        }
    };
    
    $(".tab-link").on("click keypress", function(){
        if(clickAndPress(event) === true) {
            var id = $(this).data('target')
            var par_id = $(this).data('id');
            $(".tab-content").replaceClass("d-block", "d-none");
            $(".sr-only").remove();
            $(document.getElementById(id)).replaceClass("d-none", "d-block");
            $(".tab-link").removeClass("active");
            if ($(".btn-acc-tab").hasClass("active")) {
                $(".btn-acc-tab").removeClass("active");
            };
            $(this).addClass("active");
            $(document.getElementById(par_id)).addClass("active");
            $('<span class="sr-only">(current)</span>').insertAfter(".active");
        }
    });
    $(document).ready(function(){
        var active = $(".tab-link.active").data('target');
    
        $(".acc-tab").each(function() {
          if ($(this).find('li.active').length > 0) {
            $(this).addClass("show");
          }
        });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <div class="container">
        <div class="accordion" id="accordion_Foo">
            <div class="card">
                <div class="card-header" id="heading_Foo_Bar">
                    <button class="btn btn-acc-tab" type="button" id="button_Foo_Bar" data-toggle="collapse" data-target="#collapse_Foo_Bar" aria-expanded="true" aria-controls="collapse_Foo_Bar">
                        <h4>Foo Bar</h4>
                    </button>
                </div>
                <div class="acc-tab collapse" id="collapse_Foo_Bar" aria-labeledby="heading_Foo_Bar" data-parent="#accordion_Foo">
                    <ul class="tab-list">
                        <li class="btn tab-link active" data-target="Cat" data-id="button_Foo_Bar" tabindex="0" role="button" aria-expanded="true">
                            <a class="active" href="/Foo/Foo_Bar/Cat" target="_self">Cat</a>
                        </li>
                        <span class="sr-only">(current)</span>
                        <li class="btn tab-link" data-target="Dog" data-id="button_Foo_Bar" tabindex="0" role="button" aria-expanded="true">
                            <a class="" href="/Foo/Foo_Bar/Dog" target="_self">Dog</a>
                        </li>
                        <li class="btn tab-link" data-target="Mouse" data-id="button_Foo_Bar" tabindex="0" role="button" aria-expanded="true">
                            <a class="" href="/Foo/Foo_Bar/Mouse" target="_self">Mouse</a>
                        </li>
                    </ul>
                </div>
            </div>
            <div class="card">
                <div class="card-header" id="heading_Foo_Foo">
                    <button class="btn btn-acc-tab" type="button" id="button_Foo_Bar" data-toggle="collapse" data-target="#collapse_Foo_Foo" aria-expanded="true" aria-controls="collapse_Foo_Foo">
                        <h4>Foo Foo</h4>
                    </button>
                </div>
                <div class="acc-tab collapse" id="collapse_Foo_Foo" aria-labeledby="heading_Foo_Foo" data-parent="#accordion_Foo">
                    <ul class="tab-list">
                        <li class="btn tab-link active" data-target="Bird" data-id="button_Foo_Foo" tabindex="0" role="button" aria-expanded="true">
                            <a class="active" href="/Foo/Foo_Foo/Bird" target="_self">Bird</a>
                        </li>
                        <span class="sr-only">(current)</span>
                        <li class="btn tab-link" data-target="Fish" data-id="button_Foo_Foo" tabindex="0" role="button" aria-expanded="true">
                            <a class="" href="/Foo/Foo_Foo/Fish" target="_self">Fish</a>
                        </li>
                        <li class="btn tab-link" data-target="Dinosaur" data-id="button_Foo_Foo" tabindex="0" role="button" aria-expanded="true">
                            <a class="" href="/Foo/Foo_Foo/Dinosaur" target="_self">Dinosaur</a>
                        </li>
                    </ul>
                </div>
            </div>
            <div class="card">
                <div class="card-header" id="heading_Foo_Foo">
                    <button class="btn btn-acc-tab" type="button" id="button_Foo_Bar" data-toggle="collapse" data-target="#collapse_Foo_Foo" aria-expanded="true" aria-controls="collapse_Foo_Foo">
                        <h4>Bar Foo</h4>
                    </button>
                </div>
                <div class="acc-tab collapse" id="collapse_Foo_Foo" aria-labeledby="heading_Foo_Foo" data-parent="#accordion_Foo">
                    <ul class="tab-list">
                        <li class="btn tab-link" data-target="Bird" data-id="button_Foo_Foo" tabindex="0" role="button" aria-expanded="true">
                            <a class="" href="/Foo/Foo_Foo/Bird" target="_self">Chicken</a>
                        </li>
                        <li class="btn tab-link" data-target="Fish" data-id="button_Foo_Foo" tabindex="0" role="button" aria-expanded="true">
                            <a class="" href="/Foo/Foo_Foo/Fish" target="_self">Cow</a>
                        </li>
                        <li class="btn tab-link" data-target="Dinosaur" data-id="button_Foo_Foo" tabindex="0" role="button" aria-expanded="true">
                            <a class="" href="/Foo/Foo_Foo/Dinosaur" target="_self">Lamb</a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
    Login or Signup to reply.
  2. As I can see that URL contains the same data which is required to make the corresponding acc-tab active.

    I am assuming that at a time only one acc-tab will be active (in your given HTML 2 li of 2 different acc-tab is active)

    You can do it like below:

    var url = $(location).attr('href'),
    parts = url.split("/"),
    last_part = parts[parts.length-2];
    $('#collapse_'+last_part).addClass('show');
    

    sample working snippet:

    var url = '/Foo/Foo_Bar/Cat'; // since that 
    parts = url.split("/"),
      last_part = parts[parts.length - 2];
    console.log(last_part);
    $('#collapse_' + last_part).addClass('show');
    .show {
      color: red;
      background: grey;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <div class="container">
      <div class="accordion" id="accordion_Foo">
        <div class="card">
          <div class="card-header" id="heading_Foo_Bar">
            <button class="btn btn-acc-tab" type="button" id="button_Foo_Bar" data-toggle="collapse" data-target="#collapse_Foo_Bar" aria-expanded="true" aria-controls="collapse_Foo_Bar">
                        <h4>Foo Bar</h4>
                    </button>
          </div>
          <div class="acc-tab collapse" id="collapse_Foo_Bar" aria-labeledby="heading_Foo_Bar" data-parent="#accordion_Foo">
            <ul class="tab-list">
              <li class="btn tab-link active" data-target="Cat" data-id="button_Foo_Bar" tabindex="0" role="button" aria-expanded="true">
                <a class="active" href="/Foo/Foo_Bar/Cat" target="_self">Cat</a>
              </li>
              <span class="sr-only">(current)</span>
              <li class="btn tab-link" data-target="Dog" data-id="button_Foo_Bar" tabindex="0" role="button" aria-expanded="true">
                <a class="" href="/Foo/Foo_Bar/Dog" target="_self">Dog</a>
              </li>
              <li class="btn tab-link" data-target="Mouse" data-id="button_Foo_Bar" tabindex="0" role="button" aria-expanded="true">
                <a class="" href="/Foo/Foo_Bar/Mouse" target="_self">Mouse</a>
              </li>
            </ul>
          </div>
        </div>
        <div class="card">
          <div class="card-header" id="heading_Foo_Foo">
            <button class="btn btn-acc-tab" type="button" id="button_Foo_Bar" data-toggle="collapse" data-target="#collapse_Foo_Foo" aria-expanded="true" aria-controls="collapse_Foo_Foo">
                        <h4>Foo Foo</h4>
                    </button>
          </div>
          <div class="acc-tab collapse" id="collapse_Foo_Foo" aria-labeledby="heading_Foo_Foo" data-parent="#accordion_Foo">
            <ul class="tab-list">
              <li class="btn tab-link" data-target="Bird" data-id="button_Foo_Foo" tabindex="0" role="button" aria-expanded="true">
                <a class="active" href="/Foo/Foo_Foo/Bird" target="_self">Bird</a>
              </li>
              <span class="sr-only">(current)</span>
              <li class="btn tab-link" data-target="Fish" data-id="button_Foo_Foo" tabindex="0" role="button" aria-expanded="true">
                <a class="" href="/Foo/Foo_Foo/Fish" target="_self">Fish</a>
              </li>
              <li class="btn tab-link" data-target="Dinosaur" data-id="button_Foo_Foo" tabindex="0" role="button" aria-expanded="true">
                <a class="" href="/Foo/Foo_Foo/Dinosaur" target="_self">Dinosaur</a>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
    Login or Signup to reply.
  3. You simply need to hide the children of the collapsables that lack the show class 🙂

    .collapse:not(.show) > * {
        display: none;
    }
    

    Full example:

    (function ($) {
        $.fn.replaceClass = function (pFromClass, pToClass) {
            return this.removeClass(pFromClass).addClass(pToClass);
        };
    }(jQuery));
    function clickAndPress(event) {
        if(event.type === 'click'){
            return true;
        }
        else if(event.type === 'keypress'){
            var code = event.charCode || event.keyCode;
            if((code === 32)|| (code === 13)){
                return true;
            }
        }
        else{
            return false;
        }
    };
    
    $(".tab-link").on("click keypress", function(){
        if(clickAndPress(event) === true) {
            var id = $(this).data('target')
            var par_id = $(this).data('id');
            $(".tab-content").replaceClass("d-block", "d-none");
            $(".sr-only").remove();
            $(document.getElementById(id)).replaceClass("d-none", "d-block");
            $(".tab-link").removeClass("active");
            if ($(".btn-acc-tab").hasClass("active")) {
                $(".btn-acc-tab").removeClass("active");
            };
            $(this).addClass("active");
            $(document.getElementById(par_id)).addClass("active");
            $('<span class="sr-only">(current)</span>').insertAfter(".active");
        }
    });
    $(document).ready(function(){
        var active = $(".tab-link.active").data('target');
        $(".acc-tab").addClass("show");
    });
    .collapse:not(.show) > * {
        display: none;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <div class="container">
        <div class="accordion" id="accordion_Foo">
            <div class="card">
                <div class="card-header" id="heading_Foo_Bar">
                    <button class="btn btn-acc-tab" type="button" id="button_Foo_Bar" data-toggle="collapse" data-target="#collapse_Foo_Bar" aria-expanded="true" aria-controls="collapse_Foo_Bar">
                        <h4>Foo Bar</h4>
                    </button>
                </div>
                <div class="acc-tab collapse" id="collapse_Foo_Bar" aria-labeledby="heading_Foo_Bar" data-parent="#accordion_Foo">
                    <ul class="tab-list">
                        <li class="btn tab-link active" data-target="Cat" data-id="button_Foo_Bar" tabindex="0" role="button" aria-expanded="true">
                            <a class="active" href="/Foo/Foo_Bar/Cat" target="_self">Cat</a>
                        </li>
                        <span class="sr-only">(current)</span>
                        <li class="btn tab-link" data-target="Dog" data-id="button_Foo_Bar" tabindex="0" role="button" aria-expanded="true">
                            <a class="" href="/Foo/Foo_Bar/Dog" target="_self">Dog</a>
                        </li>
                        <li class="btn tab-link" data-target="Mouse" data-id="button_Foo_Bar" tabindex="0" role="button" aria-expanded="true">
                            <a class="" href="/Foo/Foo_Bar/Mouse" target="_self">Mouse</a>
                        </li>
                    </ul>
                </div>
            </div>
            <div class="card">
                <div class="card-header" id="heading_Foo_Foo">
                    <button class="btn btn-acc-tab" type="button" id="button_Foo_Bar" data-toggle="collapse" data-target="#collapse_Foo_Foo" aria-expanded="true" aria-controls="collapse_Foo_Foo">
                        <h4>Foo Foo</h4>
                    </button>
                </div>
                <div class="acc-tab collapse" id="collapse_Foo_Foo" aria-labeledby="heading_Foo_Foo" data-parent="#accordion_Foo">
                    <ul class="tab-list">
                        <li class="btn tab-link active" data-target="Bird" data-id="button_Foo_Foo" tabindex="0" role="button" aria-expanded="true">
                            <a class="active" href="/Foo/Foo_Foo/Bird" target="_self">Bird</a>
                        </li>
                        <span class="sr-only">(current)</span>
                        <li class="btn tab-link" data-target="Fish" data-id="button_Foo_Foo" tabindex="0" role="button" aria-expanded="true">
                            <a class="" href="/Foo/Foo_Foo/Fish" target="_self">Fish</a>
                        </li>
                        <li class="btn tab-link" data-target="Dinosaur" data-id="button_Foo_Foo" tabindex="0" role="button" aria-expanded="true">
                            <a class="" href="/Foo/Foo_Foo/Dinosaur" target="_self">Dinosaur</a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search