Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap accordion: how to make entire button clickable?

i add an accordion menu to my site, using this example:

<div class="bs-example">
    <div class="panel-group" id="accordion">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h4 class="panel-title">
                <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">1. Menu number one</a>
            </h4>
        </div>
        <div id="collapseOne" class="panel-collapse collapse in">
            <div class="panel-body">
                <p>Menu number one text</p>
            </div>
        </div>
    </div>
    <div class="panel panel-default">
        <div class="panel-heading">
            <h4 class="panel-title">
                <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">2. Menu number two</a>
            </h4>
        </div>
        <div id="collapseTwo" class="panel-collapse collapse">
            <div class="panel-body">
                <p>Menu number two</p>
            </div>
        </div>
    </div>
    <div class="panel panel-default">
        <div class="panel-heading">
            <h4 class="panel-title">
                <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree">3. Menu number three</a>
            </h4>
        </div>
        <div id="collapseThree" class="panel-collapse collapse">
            <div class="panel-body">
                <p>Menu number three</p>
            </div>
        </div>
    </div>
    </div>
</div>

You can see it live here

Do you know how can i make the entire button clickable? At the moment just the title does it.

Thank you in advance

like image 854
smartmouse Avatar asked Jul 29 '15 07:07

smartmouse


People also ask

How do I set my accordion to open by default?

Go to the Accordion module settings to the Advanced tab and open the CSS ID & Classes toggle and place the custom class “pa-accordion” into the CSS Class input field.

What is data BS toggle?

data-toggle = “collapse” It is used when you want to hide a section and make it appear only when a div is clicked. Say, the div is a button, so when the button is clicked, the section that you want to collapse appears and re-appears using the button. Example: HTML.

How do I create a collapsible list in Bootstrap?

Just add data-toggle="collapse" and a data-target to the element to automatically assign control of one or more collapsible elements. 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 does accordion work Bootstrap?

A Bootstrap accordion has multiple collapsible cards, and only one of them can be shown at a time. The card is displayed or hidden when the appropriate button is clicked. If we choose a different card to view, the first one is hidden automatically.


3 Answers

Create click event .panel-heading and in the event check if .panel-heading second ancestor is <a class="" href="#colapseXXXXX" data-parent="#accordion" data-toggle="collapse"></a>. For example checking value in href. Then if it is true trigger click on the <a>.

Try this code:

$('.panel-heading').on('click', function () {
    var a = $(this).childen('.panel-title').children('a');
    var ahref = a.prop('href');
    if (ahref.indexOf('#collapse') > -1)
    {
        a.trigger("click");
    }
});

Working solution:

$(document).ready(function(){

    $('.panel-heading').on('click', function () {
        var a = $(this).find('a');
        var ahref = a.prop('href');
        if (ahref.indexOf('#collapse') > -1)
        {
            $('.panel-collapse').removeClass('in'); 
            $(this).next('div').addClass('in');
        }
    });

    $(".panel-heading a").on('click', function(event) { 
        $(this).closest('.panel-heading').trigger('click');
        return false;
    });

});;

Triggering click on causing bubbling up click event back to .panel-heading and .panel-heading was triggering click on again making infinite loop. If I try to preventDefault() I also prevent default bootstrap behavior so click didn't change anything. Solution above is the only one in my opinion.

like image 69
Sebastian Xawery Wiśniowiecki Avatar answered Nov 05 '22 12:11

Sebastian Xawery Wiśniowiecki


Just a note,

Did you try to pull the a outside of the h4 class? so instead of

    <div class="panel-heading">
        <h4 class="panel-title">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">1. Menu number one</a>
        </h4>
    </div>

try

    <div class="panel-heading">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
            <h4 class="panel-title">1. Menu number one</h4>
         </a>
    </div>
like image 33
Clyde Avatar answered Nov 05 '22 11:11

Clyde


I know this is an old thread, but i had the same problem and solved it way easier.

Just give your ".panel-title a" a display of block

If the parents divs got padding, set it to 0 and set it to your .panel-title a instead

.panel-heading{
    padding: 0;
}

.panel-title a{
    display: block;
    padding: 15px;
}

No need for .js here

like image 1
hikups Avatar answered Nov 05 '22 13:11

hikups