Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I go to anchor and open a Bootstrap accordion at the same time?

I'm using a Bootstrap accordion and it works like a charm, no problem at all. However, I'd like to have a link to one of the tabs in the accordion. The idea is that once I click on the link, I'm taken to that part of teh page (the accordion is really down in the page) and open the tab.

So I used the following:

<a href="#prizes" data-toggle="collapse" data-parent="#accordion"><i class="mdi-action-info"></i></a>

which should work. And it does in part: it opens the accordion tab just right, however, the page doesn't scroll to the anchor as it should. It just stays in the same position, but opening the accordion.

Any idea how to fix it? I have found answers related to jQuery UI accordion but nothing about BS accordion (and the jQuery UI answers didn't work either), really don't know why isn't this working

like image 583
Rick Alvarez Avatar asked Mar 29 '15 00:03

Rick Alvarez


People also ask

How do you open an accordion with an anchor link?

Click on the Add Accordion panel button. In the Title box, type the text that will be used for the link that is visible when the panel is collapsed. The link is clicked to expand or collapse the panel. You can specify a unique name for the panel in the Anchor tag box to allow deep links to expand the panel.

How do I keep my bootstrap accordion open?

Just add data-toggle="collapse" and a data-target to element, to automatically assign control of a collapsible element. The data-target attribute accepts a CSS selector to apply the collapse to. Be sure to add the class collapse to the collapsible element. If you'd like it to default open, add the additional class in .

How do you close one accordion when another opens?

How do you close one accordion when another opens? open accordian > at right side go to accordian seting > scrol down to Expand/Collapse Accordion Option On Page Load > choose Hide/close All Accordion..

How do I create a toggle div in bootstrap?

To control (show/hide) the collapsible content, add the data-toggle="collapse" attribute to an <a> or a <button> element. Then add the data-target="#id" attribute to connect the button with the collapsible content (<div id="demo">).


2 Answers

Building off of Michele's answer, here is an example where you can do an animated scroll to the div:

$(document).ready(function() {
  $('#accordionEvent').on('shown.bs.collapse', function() {
    var position = $('#accordionEvent').offset().top;
    $('HTML, BODY').animate({scrollTop: position }, 500); //500 specifies the speed
  });
});
like image 77
Chris McGlade Avatar answered Oct 21 '22 01:10

Chris McGlade


In my case I prefer to avoid adding JavaScript and decide to use two attributes (data-target and href) so that each of them has one job: 1)launch tab & 2)go to anchor:

<a data-toggle="collapse" data-parent="#accordion" data-target="#$foo" href="#$foo">

like image 44
Ignacio Ara Avatar answered Oct 21 '22 02:10

Ignacio Ara