tags:

views:

13

answers:

2

As part of a jquery function u use this ajax post statement to get some reaction from a server. I want to pass some arguments to my test page. But these must be variables and are declared on the previous lines (iid and inst)

How can i put these variables within the data object marked with the XX?

iid = $(this).attr('iid');
inst = $(this).attr('inst');
$.post("test.php", { inst: XX, iid: XX},function(data){
      alert("Data Loaded: " + data);
    });
A: 

If you want to define a property of an object where the property name is stored in a variable, you must do so after creating the object. You can't do it in the constructor.

var foo = {};
foo[iid] = "something";
foo[inst] = "something";
jQuery.post('test.php', foo, function // etc etc
David Dorward
A: 

I hope I understood your question. iid and inst should be the keys, not the values, right?

Here you go:

var iid = $(this).attr('iid'),
    inst = $(this).attr('inst'),
    data = {};

data[iid] = XX;
data[inst] = XX;

$.post("test.php", data,function(data){
      alert("Data Loaded: " + data);
});
Vincent