I have a dynamic modal as string and I’m creating this modal when you click button. (I didn’t use any template framework only with js)
so I have two date attribute in my html: data-open-hours
and data-closed-hours
and I controlled this date when my modal is opening..
if you check it out my demo you gonna see the issue.let me explain with you.
if you click See Agency 1
first, See Agency 1 modal will be creating as string
and will open with js.
so after close See Agency 1
then if you open See Agency 2
you will see See Agency 1
will open..
but if you refresh the page and if you click first See Agency 2
you will see
as soon as close See agency 2
then if you try see agency 1
and See agency 2
will be open
Which one you click first it modal has been opening
so I guess my event is wrong or my js function for creating html template
where is my mistake ?
sorry about my bad english.
var openHours;
var closedHours;
function agencyModal(modalName, modalWidth, modalHeight, openHours, closedHours) {
console.log("Open: " + openHours + " Closed hours: " + closedHours);
var html =
'<div class="modal fade agencyModal" 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">' + modalName + "</h4></div>";
html = html + '<div class="modal-body">';
html = html + '<div class="row"><div class="col-lg-12"><input type="text" class="form-control" placeholder="Adınız ve Soyadınız" name="adsoyad"></div></div>';
html = html + '<div class="row"><div class="col-lg-12"><input type="text" class="form-control" placeholder="E-posta Adresiniz" name="adsoyad"></div></div>';
html = html + '<div class="row"><div class="col-lg-12"><input type="text" class="form-control tel-number" placeholder="Telefon Numaranız" name="telnumber"></div></div>';
html = html + '<div class="row"><div class="col-lg-6"><select class="when-call form-control"><option class="call-today">Call Today</option><option class="call-tomorrow">Call Tomorrow</option></select></div><div class="col-lg-6">';
html = html + '<select class="hour-call form-control">' + getOptions(openHours, closedHours, true) + '</select></div></div>';
html = html + '<div class="row"><div class="col-lg-12"><button type="button" class="btn btn-success">Gönder</button></div></div></div>';
html = html + '<div class="modal-footer"><button type="button" class="btn btn-popup-kapat" data-dismiss="modal">Kapat</button></div>';
html = html + '</div></div></div>';
// check length and append if it is not added before
!$(".agencyModal").length && $(document.body).append(html);
$(".agencyModal").modal();
}
$(document).on("click", ".open-agency", function() {
openHours = $(this).data("open-hours");
closedHours = $(this).data("closed-hours");
agencyName = $(this).data("name");
agencyModal(agencyName, "40%", "500px", openHours, closedHours);
});
function callNow() {
return '<option class="call-now">Hemen Ara</option>';
}
function getOptions(open, close, now) {
var options = now ? callNow() : '';
console.log(open, close, now);
// get open/close time as hours only
var start = open.split(':')[0];
var end = close.split(':')[0];
// using +start will convert to a base 10 number - avoiding the problem that numbers with a leading zero are octal numbers
var dt = new Date();
var time = dt.getHours()
// loop and add an option for each
for (var h = +start; h <= +end; h++) {
if (time < h && now == true) {
options += '<option>' + h + ':00 - ' + (h + 1) + ':00 Arası</option>'
} else if (!now) {
options += '<option>' + h + ':00 - ' + (h + 1) + ':00 Arası</option>'
}
}
return options;
}
$(document).on("change", ".when-call", function(event) {
// not the most efficient way, but you can always remove 'Call now', then add it back only if needed
$(".hour-call .call-now").remove();
$('.hour-call option').remove();
if ($('.call-today').is(':selected')) {
$('.hour-call').prepend(getOptions(openHours, closedHours, true));
} else {
$('.hour-call').prepend(getOptions(openHours, closedHours, false))
}
});
.open-agency {
border: 3px solid #ccc;
display: inline-block;
padding: 12px;
font-size: 14px;
margin: 100px;
cursor: pointer;
box-shadow: 0px 0px 10px #ccc;
}
.open-agency:hover {
background: #050505;
color: #fff;
border-color: #000;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<p class="open-agency" data-toggle="modal" data-target=".agencyModal" data-open-hours="09:00" data-closed-hours="22:00" data-name="Agency 1">See Agency 1</p>
<p class="open-agency" data-toggle="modal" data-target=".agencyModal" data-open-hours="07:00" data-closed-hours="20:00" data-name="Agency 2">See Agency 2</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>
2
Answers
Just change the content of an existing template:
Your issue is in this line:
After the first time the first part returns false and so the new html is never added.
Moreover, creating a new modal on the fly does not remove the old one.
You need to remove the previously added modal before adding the new one: