I have used jQuery Datepicker successfully for some time to input two dates ("ArrivalDate" and "DepartureDate"). Now I am trying to adapt the present page to show the difference between the two. This should be revealed (jQuery slideDown) after the second date has been inputted.
My HTML is
<form action="tba.php" method="post" id="bookform" name="bookform">
<table cellspacing="3px" width="100%" border="0">
<tr>
<td align="right">
<b>Arrival Date: </b>
</td>
<td>
<input name="ArrivalDate" id="ArrivalDate" size="10">
</td>
</tr>
<tr>
<td align="right">
<b>Departure Date: </b>
</td>
<td>
<input name="DepartureDate" id="DepartureDate" size="10">
</td>
</tr>
</table>
<div id="numnights" style="display:none">
<table cellspacing="3px" width="100%" border="0">
<tr>
<td align="right" width="48.5%"> </td>
<td>That's <span id="nights"></span> nights in total</td>
</tr>
</table>
</div>
</form>
and the jQuery is
$(document).ready(function () {
//jQuery UI datepicker
$("#ArrivalDate").datepicker({
dateFormat: "d M yy",
minDate: 0,
showOtherMonths: true,
selectOtherMonths: true,
onClose: function () {
var DepDate = $('#DepartureDate');
var limitDate = $(this).datepicker('getDate');
//add 1 day to Arrival Date
limitDate.setDate(limitDate.getDate() + 1);
DepDate.datepicker('option', 'minDate', limitDate);
$(this).datepicker('option', 'minDate', minDate);
}
});
$('#DepartureDate').datepicker({
dateFormat: "d M yy",
showOtherMonths: true,
selectOtherMonths: true
});
//Reveal number of nights
$("#DepartureDate").on("change", function () {
var arr_date = $("#ArrivalDate").datepicker('getDate');
var dep_date = $("#DepartureDate").datepicker('getDate');
var diff = Math.floor((dep_date.getTime() - arr_date.getTime()) / 86400000);
$('#nights').val(diff);
$("#numnights").hide().slideDown(1200);
});
});
The problem I have is that the span (id=nights) is always blank. I have tried substituting other elements: input works p doesn't work
How can I persuade it to display a piece of text saying "That's 5 nights in total"?