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

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

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