javascript - Passing dynamic field names to jquery's each method -


i'm trying pass field name dynamically function in order form use autocomplete. call function in page keep getting error , think it's because it's trying column property literally opposed dynamically.

in php {$fieldname}. there javascript equivalent? or getting horribly wrong?

// call function index.php inputsuggetion('input.search', 'http://myapi.com/endpoint', 'title')   // function function inputsuggestion(element, endpoint, fieldname) {     $(element).on("keydown", function(event) {         $.ajax({             type: 'get',             datatype: "json",             data: {                 q: $(element).val()             },             url: endpoint,             success: function(data) {                 var apicollection = [];                 $.each(data, function(key, value) {                     apicollection.push(value.fieldname);                 });                 $(".autocomplete").autocomplete({                     source: apicollection                 });             },             error: function(jqxhr, textstatus, errorthrown) {                 console.log(json.stringify(jqxhr));                 console.log("ajax error: " + textstatus + ' : ' + errorthrown);             }         });     }); } 

thanks!

if i'm right want value of field 'title' json data , field name 'title' passed function dynamically. in case syntax value json wrong.

you need change following line of code:

from :

apicollection.push(value.fieldname); 

to :

apicollection.push(value[fieldname]);  

let me know in case of issue.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -