Why won’t foo get appended?
$.ajax({
type: 'GET',
dataType: 'json',
cache: false,
url: 'barfoochakalaka',
success:
function(response) {
$.each(response, function(index, val) {
$(this).parent().parent().append('foo');
});
}
})
Because inside
each,thisis set to the current element being iterated over (docs), so normally we definethisto be something else before we enter theeachloop:However, in this circumstance,
thisin thesuccesscallback of an AJAX request is equal to thejqXHRobject which launched the request, not theDOMelement you’re after, so we have to move thevar that = thisto even further away;