0

I have this simple array, but I cannot access the elements inside it. I have this ajax code in which I'm getting booked dates and storing them in an array. Then I'm just check if the selected date is booked or not by simple inArray but its not working. I have also tried getting the direct value but it gives me undefined.

AJAX

window.carRentalBookedOutDays = [];

var dataObj = {
    'action':'car_rental_booked_dates_request',
    'car_rental_id' : carRentalId,
    'month' : month,
    'year' : year,
    'nonce' : BYTAjax.nonce
};

$.ajax({
    url: BYTAjax.ajaxurl,
    data: dataObj,
    success:function(json) {
        // This outputs the result of the ajax request
        var bookedDates = JSON.parse(json);

        var i = 0;
        for (i = 0; i < bookedDates.length; ++i) {
            window.carRentalBookedOutDays.push(bookedDates[i].booking_date);
        }

        if (typeof(callDelegate) !== 'undefined') {
            callDelegate();
        }
    },
    error: function(errorThrown) {

    }
});

Checking Condition:

var datesArray = window.carRentalBookedOutDays;
var dateToCheck = "2017-11-03";

console.log(datesArray);

// returns undefined
console.log(datesArray[0]);

if ($.inArray(dateToCheck, datesArray) > -1) {
    dateTest = false;
    break;
}
if ($.inArray(dateToCheck2, datesArray) > -1) {
    dateTest = false;
    break;
}

Output: http://take.ms/HRK76

Zain Sohail
  • 464
  • 5
  • 22

1 Answers1

0

Something strange was happening, if I added a delay in the checking script it worked. But adding delay was not the right option.

I ended up verifying the dates in the ajax success method itself, it was working fine in there.

I think this issue is related to this

Zain Sohail
  • 464
  • 5
  • 22