Question from a JavaScript/jQuery beginner:
I'm putting the actual questions into the comments of the code to make it crystal clear:
$("document").ready(function() {
var timestamp;
$("#startDate").datepicker({
// the following is updating var timestamp
// every time the user selects a date via datepicker
onSelect: function(e) {
var dateAsObject = $(this).datepicker( "getDate" ); //the getDate method
timestamp = dateAsObject.getTime(); // timestamp of the date selected by user
console.log("user selected: " + timestamp);
return timestamp;
}
});
// how do I get the value of timestamp here,
// outside the function every time the value of
// timestamp changes inside the function?
console.log("test: " + timestamp);
// Why is the above line of code not updating/outputting anything
// when the value of timestamp changes inside the function
// and how do I get it to work/update here, outside the function?
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<form action="">
<label for="startDate">Select a date:</label>
<input type="text" id="startDate" name="startDate">
</form>