Bootstrap modal inside another modal - Close Button Issue

Viewed 387

In my JSP page, I have a modal that shows a page which has another modal (inner modal).

The first modal:

<div class="modal-dialog modal-lg"  style="background: white;  width: 90% !important">
<input type="hidden" id="report-request-data-id" value="${requestId}"/>
<div class="modal-body">
  <div id="reportDataDetailsDiv"></div>
</div>
<div class="modal-footer">
              
    <label class="btn-is float">
        <spring:message code='close.label'/>
        <input type="button" id="close_report_request_view" class="col-xs-3 col-lg-1 col-md-1" data-dismiss="modal" style="display: none;"/>
    </label>
              
    </div>
</div>

The inner modal (loaded in the div with id "reportDataDetailsDiv" above):

<div class="modal-dialog modal-lg" style="background: white;border-radius: 6px;">
<div class="modal-body">
<div class="modal-content">
<div class="col-centered col-lg-12 col-xs-12 ">


.....etc


<div class="modal-footer">
              
        <label class="btn-is float">
            <spring:message code='close.label'/>
            <input type="button" class="cus-close-modal-btn col-xs-3 col-lg-1 col-md-1" style="display: none;"/>
        </label>
              
    </div>
    </div>
    </div>
</div>

The problem happens when I click the close button in the inner modal. When I do that, both modal gets closed, but I would like the close action to be independent for each modal. How should I correctly do that?

Thank you.

1 Answers

As you are loading other modal inside reportDataDetailsDiv which is inside main-modal you can use .show() show it and whenever close button is clicked you can remove whole div which is added inside reportDataDetailsDiv.

Demo Code :

//suppose other modal is added like below
$(".addmodal").click(function() {
  //append modal undr div
  $("#reportDataDetailsDiv").html('<div class="modal-dialog modal modal-child" style="background: white;border-radius: 6px;" ><div class="modal-body"><div class="modal-content">Some message to show ....<div class="col-centered col-lg-12 col-xs-12 "><div class="modal-footer">  <label class="btn-is float"><input type="button" class="cus-close-modal-btn " value ="Close" style=""/> </label> </div></div></div></div>')
  $(".modal-child").show(); //show that modal
  $(this).hide();
  $("#close_report_request_view").prop('disabled', true);//disable main modal button
})

//onclick of close button for modal added
$(document).on('click', '.cus-close-modal-btn', function() {
  $(this).closest(".modal-child").remove(); //remove whole div
  $("#close_report_request_view").prop('disabled', false);//enable main modal buton
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<a href="#myModal" role="button" class="btn btn-primary" data-toggle="modal">Click to open modal</a>
<div class="modal-dialog modal fade modal" id="myModal" tabindex="-1" role="dialog" aria-hidden="true" style="background: white;  width: 90% !important">
  <input type="hidden" id="report-request-data-id" value="1" />
  <div class="modal-body">
    <div id="reportDataDetailsDiv"></div>
  </div>
  <button class="addmodal btn btn-primary">Click me to open other modal</button>
  <div class="modal-footer">
    <label class="btn-is float">
      Close
        <input type="button" id="close_report_request_view" class="col-xs-3 col-lg-1 col-md-1" data-dismiss="modal" style="display: none;"/>
    </label>

  </div>
</div>

Related