views:

52

answers:

2

Hi, following code:

// also tried function getDeletedDates()
var getDeletedDates = function()
{
    var s = new Array();

    $(".deleted").each(function(i, e) {
        s.push($(e).attr("data-day"));
    });
};

    $(function()
    {
        $("#delete_send").click(function() {
            alert("drin");
            $.ajax({
                  url: "delete.php",
                  type: "POST",
                  data: ({deleteDates : getDeletedDates()}),
                  dataType: "json",
                  success: function(msg){
                     alert(msg);
                  },
                  beforeSend: function(){
                      alert("Lösche folgende Urlaubstage: "+ getDeletedDates().join(", "));
                  },
                  error: function(x, s, e) {
                      alert("Fehler: " + s);
                  }
               }
            );
        });
    });

But i come into beforeSend() he always says "getDeletedDates() undefined" Why is this, i declared the function in global scope?

thanks in advance.

+5  A: 

Your function doesn't return anything, so the result will be undefined. Change the method to return the array.

UPDATE:

When you do getDeletedDates() it is evaluated to undefined, because of the lack of return result. This is why getDeletedDates() is undefined is the error message.

Matthew Abbott
ohh thank you this helped,but I'm confused why it says "getDeletedDates() is undefined"but thank you very much!
Weidling C
So accept answer.
Cipi
See updated answer for why.
Matthew Abbott
+2  A: 

You are calling your function, but the function is defined as a variable/pointer and doesn't return anything. The following modification should work (not tested):

function getDeletedDates()
{
    var s = new Array();

    $(".deleted").each(function(i, e) {
        s.push($(e).attr("data-day"));
    });

    return s;
};
Prutswonder