Bootstrap Accordion button toggle “data-parent” not working

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Bootstrap Accordion button toggle “data-parent” not working



I'm trying to create an accordion using Bootstrap 3 using the button collapsible with the data attribute, without the accordion markup. It just looks more cleaner to me.



However I cannot make the data-parent attribute to work. I want when opening a question, all others to close. I'm reading the docs ( http://getbootstrap.com/javascript/#collapse-usage ) but still cannot get it to work.



I'm using the following code:


<div class="accordion" id="myAccordion">
<button type="button" class="btn btn-danger" data-toggle="collapse" data-target="#collapsible-1" data-parent="#myAccordion">Question 1?</button>
<div id="collapsible-1" class="collapse">
<p>Etiam posuere quam ac quam. Maecenas aliquet accumsan leo. Nullam dapibus fermentum ipsum. Etiam quis quam. Integer lacinia. Nulla est.</p>
</div>
<button type="button" class="btn btn-danger" data-toggle="collapse" data-target="#collapsible-2" data-parent="#myAccordion">Question 2?</button>
<div id="collapsible-2" class="collapse">
<p>Etiam posuere quam ac quam. Maecenas aliquet accumsan leo. Nullam dapibus fermentum ipsum. Etiam quis quam. Integer lacinia. Nulla est.</p>
</div>
<button type="button" class="btn btn-danger" data-toggle="collapse" data-target="#collapsible-3" data-parent="#myAccordion">Question 3?</button>
<div id="collapsible-3" class="collapse">
<p>Etiam posuere quam ac quam. Maecenas aliquet accumsan leo. Nullam dapibus fermentum ipsum. Etiam quis quam. Integer lacinia. Nulla est.</p>
</div>
</div>



Here is the JSFiddle: http://jsfiddle.net/twinsen/AEew4/



I'll be very happy if someone points to me where I'm making a mistake :




7 Answers
7



Bootstrap 4



Use the data-parent="" attribute on the collapse element (instead of the trigger element)


data-parent=""


<div id="accordion">
<div class="card">
<div class="card-header">
<h5>
<button class="btn btn-link" data-toggle="collapse" data-target="#collapseOne">
Collapsible #1 trigger
</button>
</h5>
</div>
<div id="collapseOne" class="collapse show" data-parent="#accordion">
<div class="card-body">
Collapsible #1 element
</div>
</div>
</div>
... (more cards/collapsibles inside #accordion parent)
</div>



Bootstrap 3



See this issue on GitHub: https://github.com/twbs/bootstrap/issues/10966



There is a "bug" that makes the accordion dependent on the .panel class when using the data-parent attribute. To workaround it, you can wrap each accordion group in a 'panel' div..


.panel


data-parent



http://bootply.com/88288


<div class="accordion" id="myAccordion">
<div class="panel">
<button type="button" class="btn btn-danger" data-toggle="collapse" data-target="#collapsible-1" data-parent="#myAccordion">Question 1?</button>
<div id="collapsible-1" class="collapse">
..
</div>
</div>
<div class="panel">
<button type="button" class="btn btn-danger" data-toggle="collapse" data-target="#collapsible-2" data-parent="#myAccordion">Question 2?</button>
<div id="collapsible-2" class="collapse">
..
</div>
</div>
<div class="panel">
<button type="button" class="btn btn-danger" data-toggle="collapse" data-target="#collapsible-3" data-parent="#myAccordion">Question 3?</button>
<div id="collapsible-3" class="collapse">
...
</div>
</div>
</div>



Edit



As mentioned in the comments, each section doesn't have to be a .panel. However...


.panel


.panel


data-parent=


data-toggle=


.panel





Oh, thanks! I didn't know about this "issue". I wrap it up right away.
– Ziik
Oct 17 '13 at 12:23





In fact, you can wrap the entire group in a single .panel div. The important thing seems to be that the .panel is (1) a direct child of the data-parent and (2) the .collapse elements are direct children of a .panel.
– Blazemonger
Nov 12 '13 at 16:56


.panel


.panel


data-parent


.collapse


.panel





ah, tx! > @fat: "this is the way it is because of nesting accordions"
– ptim
Jul 10 '14 at 16:51





.panel is only required if you want accordion behaviour, not just collapse: bootply.com/5XTEIviBwp
– ptim
Jul 10 '14 at 16:59


.panel





unneeded markup, shame.
– Neil
Oct 19 '15 at 14:20




Note, not only there is dependency on .panel, it also has dependency on the DOM structure.



Make sure your elements are structured like this:


<div id="parent-id">
<div class="panel">
<a data-toggle="collapse" data-target="#opt1" data-parent="#parent-id">Control</a>
<div id="opt1" class="collapse">
...



It's basically what @Blazemonger said, but I think the hierarchy of the target element matters too. I didn't finish trying every possibility out, but basically it should work if you follow this hierarchy.



FYI, I had more layers between the control div & content div and that didn't work.



Try this. Simple solution with no dependencies.




$('[data-toggle="collapse"]').click(function()
$('.collapse.in').collapse('hide')
);





needs a little polish… but seems to work well…
– dj.cowan
Apr 16 '17 at 1:23





Best Answer...Amazing
– Vivek Saha
Aug 18 '17 at 11:53



As Blazemonger said, #parent, .panel and .collapse have to be direct descendants. However, if You can't change Your html, You can do workaround using bootstrap events and methods with the following code:


$('#your-parent .collapse').on('show.bs.collapse', function (e)
var actives = $('#your-parent').find('.in, .collapsing');
actives.each( function (index, element)
$(element).collapse('hide');
)
)





This opens panel then immediately closes it
– kernowcode
Apr 26 '16 at 9:12





@kernowcode works fine for me, take a look jsfiddle.net/y9a3a3sp
– Krzysztof Grzybek
May 15 at 20:05



If found this alteration to Krzysztof answer helped my issue


$('#' + parentId + ' .collapse').on('show.bs.collapse', function (e)
var all = $('#' + parentId).find('.collapse');
var actives = $('#' + parentId).find('.in, .collapsing');
all.each(function (index, element)
$(element).collapse('hide');
)
actives.each(function (index, element)
$(element).collapse('show');
)
)



if you have nested panels then you may also need to specify which ones by adding another class name to distinguish between them and add this to the a selector in the above JavaScript



Here is a (hopefully) universal patch I developed to fix this problem for BootStrap V3. No special requirements other than plugging in the script.


$(':not(.panel) > [data-toggle="collapse"][data-parent]').click(function()
var parent = $(this).data('parent');
var items = $('[data-toggle="collapse"][data-parent="' + parent + '"]').not(this);
items.each(function()
var target = $(this).data('target') );
);



EDIT: Below is a simplified answer which still meets my needs, and I'm now using a delegated click handler:


$(document.body).on('click', ':not(.panel) > [data-toggle="collapse"][data-parent]', function() $(this).prop('hash');
$(parent).find('.collapse.in').not(target).collapse('hide');
);



I got the same problem when toggling the accordion. But when I try to put the script block in the header block, it works for my case!!


<head>
...
<link rel="stylesheet" href="../assets/css/bootstrap.css" />
<script src="../assets/js/jquery-1.9.1.min.js" ></script>
<script src="../assets/js/bootstrap.js" ></script>
</head>





Are you magician turned designer?
– Vishal Kumar Sahu
Sep 21 '17 at 13:43






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered