2

i have a problem. I am trying to add a functions return value to a variable but it says the function is undefined. Here is my code. :

var selectedExpenseList = getSelectedExpenseIDs();

here is my function:

    function getSelectedExpenseIDs() {
            var selectedExpensesList = new Array;
            var i = 0;
            $('.expenseCheckBox:checked').each(function () {
                if ($(this)[0].id !== "checkAllExpenses") {
                    selectedExpensesList[i] = $(this)[0].id.split('_')[1];
                    ++i;
                }
            });
            return selectedExpensesList;
        }

EDIT: here is my entire function: I am trying to delete something from a list if a person has checked it.

     var selectedExpenseList;
 function actuallyDeleteTheExpense(canDeleteExpenses) 
 {
     selectedTasksList = getSelectedTaskIDs();
     var deleteTrackers = false, deleteExpenses = false;


     if (canDeleteExpenses && !canDeleteTrackers) 
     {
         $.Zebra_Dialog('Do you wish to remove Expenses?', 
         {
             'type': 'question',
             'title': 'Confirmation',
             'buttons': [
                        {
                            caption: 'Yes', callback: function () {
                                deleteTrackers = false;
                                deleteExpenses = true;
                                doTheDelete(deleteExpenses);
                            }
                        },

                        {
                            caption: 'No',
                            callback: function () {
                                doTheDelete(deleteExpenses);
                            }
                        }
                    ]
         });
     }
            }
            function doTheDelete(doIDeleteExpenses) 
            {
                if (selectedTasksList.length > 0) 
                {
                    $.ajax({
                        type: "POST",
                        //url: "/Tasks/ViewTasks.aspx/deleteTasksAndLinkedItems",
                        url: '<%=ResolveUrl("~/Expenses/ViewExpenses.aspx/deleteSelectedExpense")%>',
                        data: "{ 'TaskIDs': [" + selectedTasksList.join(',') + "], DeleteTrackers : " + doIDeleteTrackers + ", DeleteExpenses : " + doIDeleteExpenses + " }",
                        //fix data
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            var ss = data.d;
                            if (ss.length > 0) {
                                for (var i = 0; i < ss.length; ++i) {
                                    $.noty.consumeAlert({ layout: 'center', type: 'error', dismissQueue: true });
                                    alert(ss[i]);

                                }
                            }
                            $("#viewTasksGrid").flexReload();
                        },
                        error: function (data) {
                            $.noty.consumeAlert({ layout: 'center', type: 'error', dismissQueue: true, modal: true });
                            alert('Error Deleting Tasks');
                            if (window.console) {
                                console.log(data);
                            }
                        }
                    });
                } else {
                    showMessage('No tasks are selected.');
                }
            }
//end delete expense


        function getSelectedExpenseIDs() {
            var selectedExpensesList = new Array;
            var i = 0;
            $('.expenseCheckBox:checked').each(function () {
                if ($(this)[0].id !== "checkAllExpenses") {
                    selectedExpensesList[i] = $(this)[0].id.split('_')[1];
                    ++i;
                }
            });
            return selectedExpensesList;
        }
4
  • Do you mind editing the snippet in the order you have it in your code? Commented Sep 17, 2012 at 17:59
  • Where are your function call and function declaration with respect to each other? Is your function declaration perchance inside a $(document).ready(...)? Commented Sep 17, 2012 at 18:01
  • 1
    Please add the exact error message, and/or a reduced test case in a fiddle. Commented Sep 17, 2012 at 18:01
  • yes it is inside $(document).ready Commented Sep 17, 2012 at 18:11

1 Answer 1

2

I noticed that your function code is tabbed once more than your code that calls it. It's possible that your function is scoped improperly (e.g. it's declared inside another function and you're calling it from outside that function).

If your function is coming back as undefined, it's almost certainly a scoping issue. I see nothing wrong with the function itself.

For instance:

$(document).ready(function () {
    function myFunction() { 
        return 3;
    } 
});

var bob = myFunction(); //Error: myFunction is not defined.
Sign up to request clarification or add additional context in comments.

3 Comments

I've posted my entire function. I;m not sure about the scoping. I'm looking through it but I have it posted as well for others if they can find anything
Take a look at the example I added, if that structure seems familiar, then that's probably your culprit.
I think i might have figured it out. Really stupid. when I called the function getSelectedTaskID, the reason why it was undefined is because it was called getSelectedExpenseIDs() in this file..............

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.