Skip to content

Instantly share code, notes, and snippets.

@raddevon
Last active April 1, 2020 19:50
Show Gist options
  • Save raddevon/8958486 to your computer and use it in GitHub Desktop.
Save raddevon/8958486 to your computer and use it in GitHub Desktop.
Open a Bootstrap accordion control if its anchor is target on page load
// Opens accordion automatically if an accordion target is accessed from another page
// Assumes the accordion-group is the target linked to
function openAnchorAccordion() {
if (window.location.hash) {
var $target = $('body').find(window.location.hash);
if ($target.hasClass('accordion-group')) {
var $targetAccordion = $target.find('.collapse');
$targetAccordion.collapse('show');
}
}
}
openAnchorAccordion();
$("body").on("click", "a", openHashAccordion);
@parulkamdar
Copy link

It's not working for me. Can you please post the complete code with javascript.

@amreddys
Copy link

I am using this code for bootstrap 4. I just want to open that accordion tab on load. Placed it in window.onload

if (window.location.hash) {
                var jQuerytarget = jQuery('body').find(window.location.hash);
                if (jQuerytarget.hasClass('collapse')) {
                    var jQuerytargetAccordion = jQuerytarget.find('.collapse');
                    jQuerytarget.collapse('show');
                }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment