Use queried json data in a function
Posted
by SztupY
on Stack Overflow
See other posts from Stack Overflow
or by SztupY
Published on 2010-05-14T14:42:15Z
Indexed on
2010/05/14
14:54 UTC
Read the original article
Hit count: 289
I have a code similar to this:
$.ajax({
success: function(data) {
text = '';
for (var i = 0; i< data.length; i++) {
text = text + '<a href="#" id="Data_'+ i +'">' + data[i].Name + "</a><br />";
}
$("#SomeId").html(text);
for (var i = 0; i< data.length; i++) {
$("#Data_"+i).click(function() {
alert(data[i]);
RunFunction(data[i]);
return false;
});
}
}
});
This gets an array of some data in json format, then iterates through this array generating a link for each entry. Now I want to add a function for each link that will run a function that does something with this data. The problem is that the data seems to be unavailable after the ajax success function is called (although I thought that they behave like closures). What is the best way to use the queried json data later on? (I think setting it as a global variable would do the job, but I want to avoid that, mainly because this ajax request might be called multiple times)
Thanks.
© Stack Overflow or respective owner