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);
@kokoruz
Copy link

kokoruz commented Dec 15, 2014

Good afternoon. I came across your code and the description is exactly what I want to do but I can't figure out why it won't work for me. I am using Bootstrap 3.3.1.

I have altered the class from accordion-group to panel-group to match the current class naming in bootstrap and replaced the $ with jQuery to prevent conflict in wordpress.

my link is localhost/?page_id=27#collapseFour

//
function openAnchorAccordion() {
if (window.location.hash) {
var $target = $('body').find(window.location.hash);
if ($target.hasClass('panel-group')) {
var $targetAccordion = $target.find('.collapse');
$targetAccordion.collapse('show');
}
}
}

openHashAccordion();

jQuery("body").on("click", "a", openHashAccordion);
//

If you have any ideas what I need to do I would greatly appreciate it.

@SabreCat
Copy link

SabreCat commented Sep 4, 2015

For one thing, the function call needs to match the function declaration: openHashAccordion vs openAnchorAccordion

@gillespieza
Copy link

SabreCat is correct. I found putting the following code in my footer worked for me (also, make sure bootstrap.js is loaded before this, or at least don't defer the loading of that script. You should probably enqueue this for good practice.

` <script>
// 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 jQuerytarget = jQuery('body').find(window.location.hash);
//console.log( jQuerytarget );
if (jQuerytarget.hasClass('panel-collapse')) {
var jQuerytargetAccordion = jQuerytarget.find('.collapse');
console.log( jQuerytargetAccordion );
jQuerytarget.collapse('show');
}
}
}

openAnchorAccordion();

jQuery("body").on("click", "a", openAnchorAccordion);    
</script>`

@robinwilson16
Copy link

Thanks this is just what I needed

To make this work with Bootstrap 4 you need to change this line from:

if (jQuerytarget.hasClass('panel-collapse')) {

To:

if (jQuerytarget.hasClass('collapse')) {

Then it works. It would be good if it could scroll to the accordion too but I have not yet worked that part out.

@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