code-8
code-8

Reputation: 58632

Dynamically Construct an accordion, and triggering it - jQuery

I have 3 actions buttons

enter image description here

I've followed this documentation https://getbootstrap.com/docs/4.3/components/collapse/

This is my (i) button

<a data-device-mac="BCAEC5DA2D87" class="stats-modify-btn" title="Device Info" data-toggle="collapse" href="#info-BCAEC5DA2D87" role="button" aria-expanded="false" aria-controls="info-BCAEC5DA2D87">
    <i class="fa fa-info ml15"></i>
</a>

Then, I also have this

$('.stats-modify-btn').click(function() {

    console.log($(this).attr("data-device-mac"));

    var deviceMac = $(this).attr("data-device-mac");
    var title = $(this).attr("title");

    var infoAccordion = `
    <div class="collapse row stats-device-table detail-slider" id="info-${device.device_mac}">
    <div class="col-sm-6" id="device_status_value">
    ${device.display_activity}
    </div>
    </div>
    `;

    if(title == "Device Info"){
        console.log('show Device Info');

        $('#tr-'+ deviceMac).append(infoAccordion).show();
    }

});

I have no idea why the accordion is not triggering.

Is it try to triggering but the actual DOM is not yet there? How do I make sure ?

Upvotes: 2

Views: 65

Answers (1)

Swati
Swati

Reputation: 28522

You can check #tr-BCAEC5DA2D87 has any div inside it i.e : collapse or not and if length is 0 then append new div inside it .Also , you need to use this condition so that no mutliple elements are appended inside your divs.

Demo Code :

$('.stats-modify-btn').click(function() {
  var device = {
    "device_mac": "BCAEC5DA2D87"
  }
  var deviceMac = $(this).attr("data-device-mac");
  var title = $(this).attr("title");
  //check if the div has .collapse inside it
  if ($('#tr-' + deviceMac + " .collapse").length == 0) {
    var infoAccordion = `
    <div class="collapse row stats-device-table detail-slider" id="info-${device.device_mac}">
    <div class="col-sm-6" id="device_status_value">
    somethingss
    </div>
    </div>
    `;
    if (title == "Device Info") {
      $('#tr-' + deviceMac).append(infoAccordion)
    }
  }
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<a data-device-mac="BCAEC5DA2D87" class="stats-modify-btn" title="Device Info" data-toggle="collapse" href="#info-BCAEC5DA2D87" role="button" aria-expanded="false" aria-controls="info-BCAEC5DA2D87">
  <i class="fa fa-info ml15">i</i>
</a>

<div id="tr-BCAEC5DA2D87">

</div>

Upvotes: 1

Related Questions