Load cloned modal (bootstrap)

Viewed 1561

Is it possible to load a newly cloned modal with bootstrap?

I'll need to load a lot of different modals, but below is an example of trying to load just one new modal.

I've seen questions on loading cloned elements within a modal, but not fully cloned modals.

Here is a fiddle with the issue: http://jsfiddle.net/hde13s2t/6/

Steps:

  1. User click "Clone" to clone modal

  2. Javascript clones modal and renames appropriate tags to "Clone" (button and modal tags)

  3. User clicks the cloned modal --> "Launch demo modal 2"

The html all loads appropriately, but the new modal does not fire.

HTML:

<!-- Button trigger modal -->
<div id="launchmodal1">

  <button type="button" class="btn btn-primary" data-toggle="modal" id="examplemodalbutton1" data-target="#exampleModal1">
    Launch demo modal 1
  </button>

  <!-- Modal -->
  <div class="modal fade" id="exampleModal1" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          ...
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary">Save changes</button>
        </div>
      </div>
    </div>
  </div>
 </div>
</div>
<button type="button" class="btn btn-secondary" id="clicktoclone">Clone</button>

Javascript:

$(document).on("click", "#clicktoclone", function() {
    var secondmodal = $("#launchmodal1").clone();

  // updating button id and data-target for modal 2
  secondmodal.find("#examplemodalbutton1").attr("id", "examplemodalbutton2").attr("data-target", "exampleModal2").html("Launch demo modal 2");

  // updating modal id for modal 2
  secondmodal.find("#exampleModal1").attr("id", "examplemodal2");

  secondmodal.appendTo('#launchmodal2');
});
1 Answers

jQuery.clone does not clone the bootstrap events for new DOM-elements. So you have to set the modal call after the new modal was append to the DOM-tree:

$(document).on("click", "#clicktoclone", function() {
    var secondmodal = $("#launchmodal1").clone();

  // updating button id and data-target for modal 2
  secondmodal.find("#examplemodalbutton1").attr("id", "examplemodalbutton2").attr("data-target", "exampleModal2").html("Launch demo modal 2");

  // updating modal id for modal 2
  secondmodal.find("#exampleModal1").attr("id", "examplemodal2");

  secondmodal.appendTo('#launchmodal2');

  // set new click event to show the cloned modal
  $('#examplemodalbutton2').on('click', function() {
    $('#examplemodal2').modal();
  })
});

If you have multiple clones of modals, you can bind a global click event listener to all modal buttons:

  $(document).on('click', '.btn', function() {
    var targetSelector = $(this).data('target');
    $(targetSelector).modal();
  })
Related