Sizzling Code
Sizzling Code

Reputation: 6070

passing the function parameter value to the function inside that function

I have this script which works fine but i want it to move to common js file.

function commonSelect2(selector,url,id,text){
    selector.select2({
        minimumInputLength:1,
        placeholder:"Select Parent Menu",
        ajax: {
            type:"post",
            url: url,
            dataType: 'json',
            quietMillis: 100,
            data: function(term, page) {
                return {
                    term: term, //search term
                    page_limit: 10 // page size
                };
            },
            results: function(data, page ) {
                var newData = [];
                $.each(data, function (index,value) {
                    newData.push({
                        id: value.id,  //id part present in data
                        text: value.text  //string to be displayed
                    });
                });
                return { results: newData };
            }
        }
    });
}

mostly it is done except the part of id and text parameter.

Here is how i send parameters

var selector = $('#selectParentMenu');
var url = "{{base_url()}}admin/configurations/loadAllParentFormNames/";
var id = "FormID";
var text = "FormName";
commonSelect2(selector,url,id,text);

problem is that

id: value.id,  //id part present in data
text: value.text  //string to be displayed

the value.id and value.text do not recognize the id and text is of parameters.

it works if i do like this

id: value.FormID,  //id part present in data
text: value.FormName  //string to be displayed

but if i put parameters containing these values don't work.

Upvotes: 1

Views: 47

Answers (1)

grin0048
grin0048

Reputation: 574

Your id and text parameters are strings containing the names of the properties you are trying to access in your value object. To access properties dynamically like that requires the value[id] syntax.

Upvotes: 3

Related Questions