Alessandro
Alessandro

Reputation: 385

jquery pass dynamic variable name

function liveUpdate(fld,value,id) {

    $.ajax({
      type: 'POST',
      url: 'myurl.html',
      data: { fld:value, 'id': id },
      success: function(data){//console.log(data);
      }
    });

    }

i want fld to be posted as fld's value not the variable name fld? i've tried to wrap around with eval but no luck

any ideas?

thanks

Upvotes: 1

Views: 1603

Answers (3)

deceze
deceze

Reputation: 522025

var data = { id : id };
data[fld] = value;

$.ajax({ ..., data : data });

Upvotes: 0

icktoofay
icktoofay

Reputation: 129001

You could do something like this:

function liveUpdate(fld, value, id) {
    var data={id: id};
    data[fld]=value;
    $.ajax({
        type: "POST",
        url: "myurl.html",
        data: data,
        success: function(data) {
            //console.log(data);
        }
    });
}

Upvotes: 1

Adeel
Adeel

Reputation: 19228

you need to modify the following line.

data: { fld:fld, id: id },

Upvotes: 0

Related Questions