Auto completion using query data to start-with results not working fine

Auto completion using query data to start-with results not working fine

 **Original Code i am using this way:** <input type="text" id="placeSelect"/> <script> $('#placeSelect').select2({ width: '100%', allowClear: true, multiple: true, maximumSelectionSize: 1, placeholder: "Click here and start typing to search.", query: function (query) { queryData{!randomJsIden}(query); }, matcher: function (term, text) { if (text.toUpperCase().indexOf(term.toUpperCase()) == 0){ return true; } return false; } }); function queryData{!randomJsIden}(query){ Visualforce.remoting.Manager.invokeAction( '{!$RemoteAction.AutoCompleteV2_Con.getData}','{!sObjVal}','{!labelFieldVar}','{!valueFieldVar}',query.term, function(result, event){ //if success if(event.status){ var data = {results: []} data.results = result; query.callback( data); } }, {escape: true} ); } </script>

if i replace the script : Auto completion start with working fine .

$('#placeSelect').select2({ width: '100%', allowClear: true, multiple: true, maximumSelectionSize: 1, placeholder: "Click here and start typing to search.", data: [ { id: 1, text: "Ford" }, { id: 2, text: "Dodge" }, { id: 3, text: "Mercedes" }, { id: 4, text: "Jaguar" } ], matcher: function (term, text) { if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) { return true; } return false; } });

if i replace data by :

query: function (query) { queryData{!randomJsIden}(query); },

Then it is not working fine : Autocompletion is working fine,But start-with is not working fine .

I am working on this since one week .Any kind of answer and suggestion are very useful to me .

Thanks in advance .Help me at your level best.