Heng Sopheak Heng Sopheak - 16 days ago 9
Javascript Question

How to collapse one of multiple cloned element in bootstrap collaps

I want to collapse one of another element which I have cloned from the old one.
But it will collapse all the element which I have cloned.

Here is JS

$(document).on('click','#addEm,.collap', function (e) {

if($(this).is('#addEm')){

var employerForm = $(this).closest('#employer').find('#cloneEm');

var cloneDiv = employerForm.clone();
$('#employerList').append(cloneDiv);
}
if($(this).is('.collap')) {

if($(this).hasClass('glyphicon-plus')) {

$(this).removeClass('glyphicon-plus');
$(this).addClass('glyphicon-minus');
$(this).closest('#employer').find('.employerForm').collapse('hide');
}else if($(this).hasClass('glyphicon-minus')){

$(this).removeClass('glyphicon-minus');
$(this).addClass('glyphicon-plus');
$(this).closest('#employer').find('.employerForm').collapse('show');
}
}

});


HTML:

<div class="tab-pane" id="employer">

<div class="panel-body ">
<div class="text-right"><a id="addEm" class="btn btn-danger btn-md"> Add </a></div>
</div>

<div id="employerList">
</div>

<div class="panel-heading" id="cloneEm" style="margin-bottom: 15px;">
<div class="text-right"><a class="collap btn btn-danger btn-xs glyphicon glyphicon-plus"> </a></div>
<div class="panel-body">
<div class="collapse employerForm">

asdfasdfasdf

</div>
</div>
</div>


</div>

Answer

Seems your selector is targeting #employer which is the list wrapping around all of your links.

Targeting #cloneEm instead seems to work.

$(document).on('click', '#addEm,.collap', function(e) {

  if ($(this).is('#addEm')) {

    var employerForm = $(this).closest('#employer').find('#cloneEm');

    var cloneDiv = employerForm.clone();
    $('#employerList').append(cloneDiv);
  }
  if ($(this).is('.collap')) {

    if ($(this).hasClass('glyphicon-plus')) {
      $(this).removeClass('glyphicon-plus');
      $(this).addClass('glyphicon-minus');

      $(this).closest('#cloneEm').find('.employerForm').collapse('hide');
    } else if ($(this).hasClass('glyphicon-minus')) {
      $(this).removeClass('glyphicon-minus');
      $(this).addClass('glyphicon-plus');
      $(this).closest('#cloneEm').find('.employerForm').collapse('show');
    }
  }
});
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.5/css/bootstrap-flex.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.5/js/bootstrap.js"></script>

<div class="tab-pane" id="employer">

  <div class="panel-body ">
    <div class="text-right"><a id="addEm" class="btn btn-danger btn-md"> Add </a></div>
  </div>

  <div id="employerList">
  </div>

  <div class="panel-heading" id="cloneEm" style="margin-bottom: 15px;">
    <div class="text-right">
      <a class="collap btn btn-danger btn-xs glyphicon glyphicon-plus"> </a>
    </div>
    <div class="panel-body">
      <div class="collapse employerForm">

        asdfasdfasdf

      </div>
    </div>
  </div>


</div>