I really couldn’t do that so I need to learn how to do that
I want to control time with jquery or javascript doesn’t matter which one it is.I want to explain my scenario is:
I have agency page and my all agency have modal and theese modal has open-hours
and closed-hours
if you check it out my example..
and if my data-open-hour
for example is 09:00
but if time is 10:00
now then remove 09:00
option and make first 10:00
and if data-closed-hours
is 20:00
and if time past 20:00
then make disabled Call Today
function agencyModal(modalTitle, 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">' + modalTitle + "</h4></div>";
html = html + '<div class="modal-body">';
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></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() {
var openHours = $(this).data("open-hours");
var closedHours = $(this).data("closed-hours");
agencyModal("Test Title ", "70%", "500px", openHours, closedHours);
});
function callNow() {
return '<option class="call-now">Call Now</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
// loop and add an option for each
for (var h = +start; h <= +end; h++) {
options += '<option>Call at '+h+':00</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();
if($('.call-today').is(':selected'))
$('.hour-call').prepend(callNow());
});
.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">See Agency</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>
3
Answers
Well if you want to display only hours that are higher than the actual hour of the day, you need to test the iterated hour in your
getOptions()
function.This is how should be the loop:
Demo:
You can get the time from the
Date()
object like so:And then update your for loop to start only from
currentHour + 1
:For the ‘Call Today’ to be disabled you could check the date in the
agencyModal()
function and add thedisabled
keyword to the<option>
.However, in order to change back to the full list of open hours when ‘Call Tomorrow’ is selected, you’d need an event listener for the first
<select>
and then apply the relevant changes from inside that function.I change a bit in your code
This will now check the current Hour and remove any before that.
I’ve also changed this bit
I will now reset the options when your change the select.