4

I'm trying to return the value of a function as an object property (as opposed to the function itself). This is my code right now, but it breaks when I try to access option_list[0][0].label or .value within my jQuery plugin. Thoughts?

$('#new').create({
    option_list:function(){return [
        [
            {label:'option1', value:'1'},
            {label:'option2', value:'2'},
            {label:'option3', value:'3'}
        ]
    ];}
});

2 Answers 2

8

You need to actually invoke the function, like so ...

$('#new').create({
    option_list:(function(){return [
        [
            {label:'option1', value:'1'},
            {label:'option2', value:'2'},
            {label:'option3', value:'3'}
        ]
    ];}())
});
Sign up to request clarification or add additional context in comments.

Comments

-1

Maybe I get you wrong but why do you want to have such a multidimensional array?

Let the function give you an object with an encapsulated array:

$('#new').create({
    option_list:function(){ return {
        [
            {label:'option1', value:'1'},
            {label:'option2', value:'2'},
            {label:'option3', value:'3'}
        ]
    };}
});

1 Comment

without full context, it seems superfluous, but it's part of a bigger form-generation schema. is there a reason returning and object vs. an array would be better?

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.