javascript - Access DOM element inside $.getJSON -
i'm developing application iterates on dom elements, , performs ajax request each 1 of elements. problem cannot access each dom element inside callback function.
$('.class').each(function() { $.getjson(url, function(data) { $(this).attr('id', data.id); // $(this) not accessible }); }); is there way solve this?
you should use variable, value of this changes each function call, , inside callback $.getjson this no longer element.
$('.class').each(function() { var self = this; $.getjson(url, function(data) { self.id = data.id; }); }); another option built in arguments in each()
$('.class').each(function(index, element) { $.getjson(url, function(data) { element.id = data.id; }); });
Comments
Post a Comment