I need the help of the experts on here.
When I call the function v9_insert_comments(‘1’) the alert(sdate) triggers before I even get to select a date in the datepicker.
My goal is to have the v9_insert_comments() check to see if the sdate is null or not. If its null then just { return } if not { do something else }.
The current case if that sdate is always null because the datepicker never gets a chance to set it.
var sdate
//========================================================================
function v9_insert_comments(x) {
//========================================================================
if (x == 1) {
select_date()
alert(sdate)
}
}//end of function
//========================================================================
function select_date() {
//========================================================================
$('#dd').dialog({
autoOpen: true,
modal: true,
overlay: {
opacity: 0.5,
background: 'black'
},
title: "title",
height: 265,
width: 235,
draggable: false,
resizable: false
}); //end of dialog
$('#d1').datepicker({
onSelect: function() {
sdate = $(this).val();
$("#dd").dialog("close");
}
});
} //end of function
Body:
<!-- START OF DATE SELECTOR DIALOG -->
<div style="display:none" id="dd">
<div id="d1"></div>
</div>
<!-- END OF DATE SELECTOR DIALOG -->
In your
select_datemethod you’re just setting stuff up. The call to$('#dd').dialog()does all the jQuery UI magic to create a dialog, by updating the DOM and wiring things up. Then you call$('#d1').datepicker(), which sets up a datepicker in a similar way. It’s just saying “this div is now a dialog, and this div is now a datepicker”. It’s not waiting for any user interaction. Yourv9_insert_commentsfunction is assuming things are happening synchronously, whereas they’re actually happening asynchronously.After these things are set up, you immediately ask for the value of a variable that hasn’t been set yet. It hasn’t been set because the anonymous function you supplied for the datepicker’s
onSelectevent hasn’t fired yet. This function will only be called when something has been selected in the datepicker. So if you want to do something with the selected date, do it in the event handler.You could move your alert to the event handler, or you could call
v9_insert_commentsfrom the event handler – but in this case you should move the call toselect_dateelsewhere. The dialog and datepicker only need to be set up once.