Question

[Solved] bootstrap collapse not working when creating dyanmically

I am fetching my content via ajax call and creating all the collapsible panel on run time.
but toggle functionality is not working in this way. I am using backbone marionette collection and item view for creating my panel-group.

I checked with static content and it works fine with static data.

I have checked few links but in my case nothing seems to work.

How to make Twitter Bootstrap Collapse work on dynamically loaded html using ajax

https://github.com/twbs/bootstrap/issues/2274

Here is my html looks like:

<div id="accordion" class="panel-group">
    <div class="panel panel-default">
        <div class="panel-heading">
            <ul class="home_nav" data-toggle="collapse" data-parent="#accordion"
                data-target="NewsMaintenanceMenu">
                ...
            </ul>
            <span class=""><i class="glyphicon glyphicon-chevron-right"></i></span>
        </div>
        <div id="NewsMaintenanceMenu" class="panel-collapse collapse">
            <div class="panel-body">
                <table class="table table-striped">
                    <tbody>
                        ....
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

Solution #1:

I have used jQuery for time being.

   $(".panel").on("click", function(e){
              var $_target =  $(e.currentTarget);
              var $_panelBody = $_target.find(".panel-collapse");
              if($_panelBody){
                $_panelBody.collapse('toggle')
              }
        })
Respondent: Aviral Jain

Solution #2:

Another way to accomplish this…

function collapseOnClick(collapseTrigger){
    /**
   * Manually add collapse on click event.
   * 
   * Because dynamically added Bootstrap collapse elements don't
   * work automatically for me most of the time.
   * 
   * 'data-target' is a selector for the collapsing element as per
   * the Bootstrap documentation. 
   * https://getbootstrap.com/docs/4.3/components/collapse/#via-data-attributes
   *
   * @param {jQuery} collapseTrigger Trigger element for the collapse.
   * 
   */ 
    var target = collapseTrigger.attr("data-target")
    collapseTrigger.on('click',function(){
        $(target).collapse('toggle')
    })
}

Respondent: Chad Lowe

Solution #3:

Call .collapse() on the newly created panel on ajax success. If you don’t know how to do it, please provide a code snippet.

Respondent: rdiz

Solution #4:

/*** I created dynamic Id’s for dynamic panel-body*************/

<div class="col-sm-4" ng-repeat="Category in categoryList">
<!-- Category -->
<div class="dropdown">
    <button class="dropbtn" ng-bind="Category.name"></button>
    <div class="dropdown-content">
        <a href="" title="">Business <span class="pull-right">13</span></a>
        <a href="" title="">Technology <span class="pull-right">13</span></a>
        <a href="" title="">Web <span class="pull-right">13</span></a>
        <a href="" title="">Ecommerce <span class="pull-right">13</span></a>
        <div class="panel-group" id="accordion">
            <div class="panel panel-default">
              <!-- <div class="panel-heading"> -->
                <div class="panel-title">

        <a href="#{{Category.name}}" data-toggle="collapse" data-parent="#accordion" title="">View More <span class="pull-right"></span></a>
       </div>
    <!-- </div> -->
    <div id="{{Category.name}}" class="panel-collapse collapse ">
        <div class="panel-body">
                <a href="" title="">Business <span class="pull-right">13</span></a>
                <a href="" title="">Technology <span class="pull-right">13</span></a>
                <a href="" title="">Web <span class="pull-right">13</span></a>
                <a href="" title="">Ecommerce <span class="pull-right">13</span></a>
        </div>
      </div>
      </div>
      </div>
      </div>
      </div>
    </div>
Respondent: Sunil Bijlwan

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy