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

Popular posts from this blog

C# random value from dictionary and tuple -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -

cgi - How do I interpret URLs without extension as files rather than missing directories in nginx? -