I've read the documentation regarding the context setting of jQuery.ajax().
When the DOM manipulation is triggered outside of the ajax call, the DOM is successfully affected:
var element = $('#logo');
element.addClass('good');
console.log(element);
$.ajax({
context: element,
url: 'myfile.json',
type:'get',
dataType:'json',
success: function(data) {
//reserved
}
});
When I move the DOM manipulation inside the ajax call, the expected result shows in the console output but is not actually reflected in the DOM:
var element = $('#logo');
$.ajax({
context: element,
url: 'myfile.json',
type:'get',
dataType:'json',
success: function(data) {
$(this).addClass('good');
console.log($(this));
}
});
// [h1#logo.good, selector: "#logo", context: document]
I have also tried referencing 'element' instead of $(this) without success:
var element = $('#logo');
$.ajax({
context: element,
url: 'myfile.json',
type:'get',
dataType:'json',
success: function(data) {
element.addClass('good');
console.log(element);
}
});
// [h1#logo.good, selector: "#logo", context: document]
It seems odd that the context in the console output is 'document' rather than '#logo'. In any case, the element reference seems to get lost as soon as it's inside the ajax call.
I have tried all three of the solutions in the accepted answer of a similar question to no avail.
Any ideas? I'm going gray.