1

How can I retrieve petKeys and employeeKey values by using below mentioned jQuery functions ?

var whenSelectDateFromCalendar = function () {
    initKeyValues();
    petKeys = ? employeeKey = ?
};


var initKeyValues = function () {
    var petKeys = $('#pets input:checked').map(function () {
        return $(this).val();
    }).get().join('+');
    var employeeKey = $('#employee input:checked').map(function () {
        return $(this).val();
    }).get().join('+');
}
3
  • You must change the initKeyValues function. Commented Sep 10, 2013 at 10:17
  • @dystroy can you tell me How ? Commented Sep 10, 2013 at 10:18
  • Just send your local petKeys and employeeKey (that you noted with ?) over as a parameters to the initKeyValues function to fill their values for you. Commented Sep 10, 2013 at 10:20

2 Answers 2

4

You can return both values in an object. Try this:

var whenSelectDateFromCalendar = function () {
    var keys = initKeyValues();

    // use the object returned above to set the vars here
    petKeys = keys.petKeys;
    employeeKey = keys.employeeKey;
};


var initKeyValues = function () {
    var petKeys = $('#pets input:checked').map(function () {
        return $(this).val();
    }).get().join('+');
    var employeeKey = $('#employee input:checked').map(function () {
        return $(this).val();
    }).get().join('+');

    // return the object containing both values
    return { 
        employeeKey: employeeKey,
        petKeys: petKeys
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Option 1 - Return multiple results within an object

var initKeyValues = function () {
    var petKeys = $('#pets input:checked').map(function () {
        return $(this).val();
    }).get().join('+');

    var employeeKey = $('#employee input:checked').map(function () { return $(this).val(); }).get().join('+');}

    return {"petKeys": petKeys,  "employeeKey": employeeKey}
};

var whenSelectDateFromCalendar = function () {
    var result = initKeyValues();

    petKeys = result.petKeys;
    employeeKey = result.employeeKey;   

    // or even...

    petKeys = result["petKeys"];
    employeeKey = result["employeeKey"];
};

Option 2: Return multiple results within an array

var initKeyValues = function () {
    var petKeys = $('#pets input:checked').map(function () {
        return $(this).val();
    }).get().join('+');

    var employeeKey = $('#employee input:checked').map(function () { return $(this).val(); }).get().join('+');}

    return [petKeys, employeeKey];
};

var whenSelectDateFromCalendar = function () {
    var result = initKeyValues();

    petKeys = result[0];
    employeeKey = result[1];
};

Comments

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.