I've tried every permutation of checking for this logic to work and I cannot find the right syntax.
I have the following bit of Javascript:
var d = $("#myDatepicker1").datepicker("getDate");
if ($.isEmptyObject(d)) {
$("#dailySummaryDateSelector").datepicker("setDate", new Date());
}
When the page loads and d
is evaluated Firebug shows it as Object [ ]
In the if
statement I've tried:
if (d == null) {
if (d == {}) {
if (d == Object()) {
if ($.isEmptyObject(d)) {
if (d.isEmptyObject()) {
None of these work. As far as I can tell Object [ ]
is an empty object so how do I actually test for that and get the contents of the if
statement to execute?
I've tried every permutation of checking for this logic to work and I cannot find the right syntax.
I have the following bit of Javascript:
var d = $("#myDatepicker1").datepicker("getDate");
if ($.isEmptyObject(d)) {
$("#dailySummaryDateSelector").datepicker("setDate", new Date());
}
When the page loads and d
is evaluated Firebug shows it as Object [ ]
In the if
statement I've tried:
if (d == null) {
if (d == {}) {
if (d == Object()) {
if ($.isEmptyObject(d)) {
if (d.isEmptyObject()) {
None of these work. As far as I can tell Object [ ]
is an empty object so how do I actually test for that and get the contents of the if
statement to execute?
-
2
Strange.
(d == null)
should work. You do realize you are calling two ids ($("#myDatepicker1")
and$("#dailySummaryDateSelector")
), right? – acdcjunior Commented May 12, 2013 at 19:46 - Well spotted!! I cannot believe I missed that! (d == null) is indeed working now! – Jammer Commented May 12, 2013 at 19:51
- A datepicker is initialized with the current date as a startdate, so it should'nt normally return null at all, unless you explicitly set it to have no date selected. If the problem is trying to get the date from an object that does'nt exist because you did'nt check your selectors, @acdcjunior should add that as an answer, and you should accept it, as it's well spotted. – adeneo Commented May 12, 2013 at 19:54
- 1 So who's down voting this? – Jammer Commented May 12, 2013 at 21:09
2 Answers
Reset to default 6getDate()
Returns: Date
Returns the current date for the datepicker or null if no date has been selected. This method does not accept any arguments.
if($("#myDatepicker1").datepicker("getDate") === null) {
alert("empty");
}
For anyone that missed it my code was broken!
var d = $("#myDatepicker1").datepicker("getDate");
if ($.isEmptyObject(d)) {
$("#dailySummaryDateSelector").datepicker("setDate", new Date());
}
Should have been:
var d = $("#dailySummaryDateSelector").datepicker("getDate");
if ($.isEmptyObject(d)) {
$("#dailySummaryDateSelector").datepicker("setDate", new Date());
}
Now this works:
var d = $("#dailySummaryDateSelector").datepicker("getDate");
if (d == null) {
$("#dailySummaryDateSelector").datepicker("setDate", new Date());
}