Lawrence Edel Lawrence Edel - 1 year ago 69
Javascript Question

How to use a variable name in ajax jQuery

I have the following code which is part of a autocomplete ajax query. My code returns a JSON it's working fine with the attached code, however I would like to use a variable in place of the item*.d_name* ie. I would like to make the auto complete function become more a boilerplate solution in which I can pass a variable to this function and it would substitute the item*.d_name*, with the variable's value. I am having difficulty in trying to figure out how to do this.

success: function(data) {
json = $.parseJSON(data);

response($.map(json, function(item) {
return {
label: item.d_name ,
value: item.d_name


Answer Source

you can use this way,

var KEY = 'd_name'; // declare in somewhere or function param

return {
  label:  item[KEY],
  value:  item[KEY]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download