I am fairly new to web dev. I've spent several days working on this (embarrassing) and researched everything I can think of.
I'm using Rails 5, bootstrap 3 and jquery 2.1.3.
I have a bootstrap accordion. Within the various collapse content divs I want links to content that resides in the same accordion but a different collapse panel.
In the current collapse panel I have a link like this:
<a class="self-link" href="#buried_sub_anchor">Buried Sub Anchor</a>
In a different collapse panel I have a content div like this:
<div class="anchor" id="buried_sub_anchor">
My jquery code to handle the click is:
$('.self-link').on('click', function() {expandCollapseWithHash(this.hash); });
function expandCollapseWithHash(hash) {
var $collapses = $(hash).parents('.collapse');
if ($collapses.length > 0) {
var $collapse = $collapses.first()
$collapse.collapse('show');
}
}
When the .collapse('show') is called I'm expecting bootstrap to magically close the current panel and open the target. Then, once that transition is done I'm expecting the 'shown' event to fire, which I handle like this:
$('.collapse').on('shown.bs.collapse', function() {
if (location.hash) {
$(location.hash).show();
}
});
I'm expecting the .show() call to jump the page right to the anchored div. But no dice.
To summarize, when I click on the link I want:
Instead, always:
My questions are:
$collapse.collapse('show')
call.Here is a fiddle. To reproduce:
Well, I stuck at it and finally have a solution. The only thing I was doing wrong was this line $(location.hash).show();
needed to instead be location.href = location.hash;
.
That makes it so it jumps to show the desired div at the very top of the page just below the header.
Here's the updated fiddle.
Within, click the "More Details" panel header, then click the link within that panel (i.e. "Buried Sub Anchor"). That will expand the "Miscellaneous" panel header and jump the page right to the "Buried Sub Anchor text" div.
Along the journey I picked up something else of value. If the above-mentioned code change was all I made (i.e. $(location.hash).show();
-> location.href = location.hash;
), when clicking on the link it would expand the target panel and jump to where you want to go. But it would also leave the panel expanded / shown that you just left as well.
The reason was not obvious to me and is fixed with the following code:
$('#accordion .panel-collapse').collapse({
parent: '#accordion',
toggle: false
});
I found that code on this github page. It initializes the individual accordion collapse elements because
the data api...lazy instantiates, something which .collapse('show') doesn't take into account.
So now in the updated fiddle when you click the link it closes the leaving panel, opens the target panel and jumps the page right to the target div, just the way I wanted.