Curious: is it possible to have dynamic Ajax data variable names? Curious: is it possible to have dynamic Ajax data variable names? ajax ajax

Curious: is it possible to have dynamic Ajax data variable names?


If I understand you correctly, this is what you're after:

jQuery.fn.updateChanges = function(){   this.bind('blur',function(){          var data = {};      data[$(this).attr("name")] = $(this).val();      $.post('/ajax/updateValue.php',        data,        function(ret){           if (ret=='success') alert("all good!");        }       }    }

Your main issue was attempting to use a dynamic value using the object literal notation, something you cannot do without reverting to really bad practices (like using eval() where eval() doesn't belong ;).

var myObj = { 'n1': 'v1' };

is equivalent to:

var myObj = {}; // {} = new Object();myObj['n1'] = 'v1';

is equivalent to:

var myObj = {},    myKey = 'n1',    myVal = 'v1';myObj[myKey] = myVal;


try with this:

jQuery.fn.updateChanges = function(){   this.bind('blur',function(){      var inputName = $(this).attr("id");      var inputValue = $(this).val();      var data = eval("{"+ inputName  +":"+ inputValue  +" }");      alert("send: "+ inputName+"\n"+data[inputName]);      $.post('/ajax/updateValue.php',        data, function(ret){           if (ret=='success') alert("all good!");        }   }}