1

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.

Fallen
  • 4,435
  • 2
  • 26
  • 46
Sizzling Code
  • 5,932
  • 18
  • 81
  • 138
  • 2
    Try `value[id]` and `value[text]` – grin0048 Aug 24 '14 at 21:23
  • @grin0048 OMG, and i thought it might not be possible..i wonder how good are you guys.. Many Thanks, it works.. Plz if possible explain why it works like in array form but not the way as it was intended to. `value.FormID` was working fine but when gave value of `FormID` to variable `text` i now have to use as like array. May i know what changed? – Sizzling Code Aug 24 '14 at 21:27

1 Answers1

3

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.

grin0048
  • 534
  • 5
  • 13
  • At first I didn't understand, this other SO question explains it further and now I get it. http://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets – nwolybug Aug 24 '14 at 21:40
  • Problem is solved but i still don't get it. why then `value.FormID` and `value.FormName` worked. Seems like javascript/jQuery is not easy to learn. – Sizzling Code Aug 24 '14 at 21:40
  • Think of it like this. `value.FormID` is the same as `value['FormId']`. `value.id` is the same as `value['id']`... that's why it doesn't work, it's looking for a property called `id`. – grin0048 Aug 24 '14 at 21:48