I have this JSON Ajax function
$.ajax({
url: "?page=" + page,
dataType: "json",
success: function(data) {
$.each(data, function() {
$("#feed").append("<li>" + this.message + "</li>");
});
}
});
However, I would like to be able to pass a variable into it, so this.message could also be this.image, or this.link. I tried doing this.
$.each(data, function() {
$("#feed").append("<li>" + myVar + "</li>");
});
and setting myVar = 'this.picture', however it treated myVar as a string and it didn’t work. How can I pass in a variable so I can achieve my goal?
Thanks in advance!
Try:
where
myVar = 'picture', or whatever other field it is that you want to display.This is the standard syntax for accessing a named property of an object when the name is itself a variable rather than a literal token.