Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent panel collapse on heading child element click

I have prepared a demo here.

I want to toggle the panel body by clicking on the panel heading. However if there are buttons in the heading, I would like them to not toggle the panel body, how can I do this?

Source code:

HTML

<div class="panel panel-default">
    <div class="panel-heading clearfix"  data-toggle="collapse" data-target="#body">
        <span>Text</span>
        <button class="btn btn-default pull-left">Button</button>
    </div>

    <div class="panel-body collapse" id="body">
        asd
    </div>
</div>

P.S.: I apologize if this has been asked already, it's hard for me to form a title to find this problem.

like image 257
php_nub_qq Avatar asked Jul 14 '15 14:07

php_nub_qq


2 Answers

$('.panel-heading .btn').click(function (e) {
    e.stopPropagation();
});

Demo

Of course, this could affect the intended function of the button as well. Explain what it will do for a better answer.

like image 137
isherwood Avatar answered Oct 17 '22 12:10

isherwood


You might want to use stopPropagation

And use javascript to achieve it:

window.noCollapse = function(e) {
    e.stopPropagation();
}

Check this fiddle

Example

like image 2
Danny22 Avatar answered Oct 17 '22 11:10

Danny22