Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having a clickable element in a Bootstrap accordion header

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?

like image 302
Aaron Reba Avatar asked Sep 25 '12 16:09

Aaron Reba


People also ask

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.

How do you expand and collapse accordion on button click?

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.

How do you add plus/minus to an accordion?

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.


2 Answers

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();
});
like image 150
Moolie Avatar answered Sep 30 '22 15:09

Moolie


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; }

like image 30
Raghavendra Karunanidhi Avatar answered Sep 30 '22 17:09

Raghavendra Karunanidhi