I have Open Modal button and when you click button, my modal has been creating with jquery, everything is okey so far but I guess my method is wrong because I noticed a bug is after open the modal if you close and if you open again you will see it has been creating 2 times,3 times,4 times….
so where is my mistake ? and another question is how can I send my parameters default value ?
function generateModal(modalTitle, modalWidth, modalHeight, iframeUrl, iframeWidth, iframeHeight) {
var html =
'<div class="modal fade mapModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"><div class="modal-dialog" role="document" style="width:' + modalWidth + ';height:' + modalHeight + '"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>';
html = html + '<h4 class="modal-title" id="myModalLabel">' + modalTitle + "</h4></div>";
html = html + '<div class="modal-body"><iframe src="' + iframeUrl + '" width="' + iframeWidth + '" height="' + iframeHeight + '" allowfullscreen></iframe></div>';
html = html + "</div></div></div>";
$(document.body).append(html);
$(".mapModal").modal();
}
$(document).on("click", ".open-modal", function() {
generateModal("Modal deneme ", "60%", "500px", "https://www.google.com/maps/embed?pb=!1m10!1m8!1m3!1d12047.436573933934!2d29.098413750000002!3d40.984565100000005!3m2!1i1024!2i768!4f13.1!5e0!3m2!1str!2str!4v1463140023736", "100%", "auto");
});
.open-modal {
border: 3px solid #ccc;
display: inline-block;
padding: 12px;
font-size: 14px;
margin: 100px;
cursor: pointer;
box-shadow: 0px 0px 10px #ccc;
}
.open-modal:hover {
background: #050505;
color: #fff;
border-color: #000;
}
.modal-dialog iframe {
width: 100%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<p class="open-modal" data-toggle="modal" data-target=".mapModal" aria-expanded="false">Open Modal</p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
4
Answers
You generate a new modal each time you click the button. If you could add a handler that gets executed once the modal is closed again, then you can fix it by simply removing the element.
You could use jQuery’s
.remove()
-function (notice that your modal needs an ID for that to work correctly).Instead of
append
you can use.html()
.Or you can remove last appended div using
.remove()
or you can use
replaceWith()
Because every time it is clicked it creates a new modal box so check your modal length and create if it is not created before like,
For the default values you can check parameters in your function like,
In you case you append the
html
.so it increased each time of click .so You need add withparent
element for whole appendhtml
.Then remove thatparent
element each time of click before the append html