I'm trying to use an accordion and have a checkbox inside of a header. However, when I click a checkbox in an accordion header, it collapses or expands the accordion. Is there a way to click the checkbox without the accordion expanding/collapsing?
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.
By default, accordion items expand or collapse by clicking the accordion item header or clicking expand/collapse icon in accordion header. You can also expand or collapse the accordion items through external button click.
Simply put the span tags into your HTML where you want the plus or minus to be, and then add the css rule to your stylesheet. If using bootstrap 3 or earlier, you can use glyphicons instead of the plain text plus and minus I used. This seems like the simplest solution.
The click bubbles up to the accordion header, so another possibility (besides moving the checkbox outside the toggle as suggested by others) is to stop the bubbling.
Use some JQuery like:
$("input[type=checkbox]").on("click", function(event) {
event.stopPropagation();
});
or with a shorter checkbox selector:
$(":checkbox").on("click", function(event) {
event.stopPropagation();
});
You have to place the checkbox outside the accordion-toggle but inside the accordion-heading class, i.e.,
<div class="accordion-heading">
<input type="checkbox" name="box"/>
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordionParent" href="#collapseone">
<p>Heading1</p>
</a>
</div>
and add the css .accordion-heading a.accordion-toggle { display: inline-block; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With