this
is set by each function when it is invoked. Your code looks like this:
// OUTSIDE the callback
var options = {
success: function() {
// INSIDE the callback
$(this).parents('.trm_expense_cats').find('.itemlist').append('<li>Test</li>');
alert('ok');
}
};
// OUTSIDE the callback
$('#formSEIncome').ajaxForm(options)
You probably expect that this
is the same value both inside and outside the callback, but it is not. The callback sets its own value of this
depending on how it is invoked. Here, the callback decides the value of this
inside the callback when it runs.
If you want to save your "outer" this
, see $(this) inside of AJAX success not working for how to use $.proxy
. You can also save your outer this
in a variable (often named that
) outside your callback. Since JavaScript functions have access to the variables of their containing functions, the callback will have access to that
:
// OUTSIDE the callback
// save outer this
var that = this;
var options = {
success: function() {
// INSIDE the callback
// we use that, not this, to get the outer this
$(that).parents('.trm_expense_cats').find('.itemlist').append('<li>Test</li>');
alert('ok');
}
};
// OUTSIDE the callback
$('#formSEIncome').ajaxForm(options)