0

I am using ajaxform to handle my form submissions and have encountered a problem with the option variable. I am trying to have the success callback append some HTML to a relative element, as such I am using the $(this) approach as I would do normally. I can't get it to work, am I missing something simple here? Is there any reason why $(this) will not work? The plugin url is http://malsup.com/jquery/form/

Thanks in advance

A

The selector references the element so on submit the ajaxform plugin fires. It is as follows $('#formSEIncome').ajaxForm(options)

The options jQuery is as follows:

var options = {             
       success: function() { 
       $(this).parents('.trm_expense_cats').find('.itemlist').append('<li>Test</li>');
        alert('ok');
    }   // post-submit callback 
};
4
  • 2
    what $(this) refer? Show ur full ajax code? Commented Jul 29, 2013 at 13:28
  • Try logging this. I don't think it is what you expect. You may have to save $(this) to a variable outside the callback and use that variable inside the callback. Commented Jul 29, 2013 at 13:29
  • Likely duplicate of $(this) inside of AJAX success not working, but there's not enough info here to say for sure. Commented Jul 29, 2013 at 13:30
  • Sorry please see above edit. Commented Jul 29, 2013 at 13:33

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your reply. So to clarify, could I do the following: var options = { $this: $(this), success: function() { options.$this.parents('.trm_expense_cats').find('.itemlist').append('<li>Test</li>'); alert('ok'); } // post-submit callback };
thanks for taking the time to help out. I have tried that and console log tells me ... Uncaught ReferenceError: $this is not defined. I can't see how this is true given the above code?
$this is undefined. The $this property of options is defined. In your success function, are you trying to use options.$this (good, options is defined and has a property called $this) or just $this (not good, not defined)?
Sorry yes, it is defined now (I had $this in console log rather than options.$this) I have fixed this but it won't append to the suggested element eg... var options = { $this: $(this) ,success: function() { options.$this.append('<div>Test</div>'); console.log(options.$this); } }; I would have hoped this would append the DIV to #formSEIncome. I think I'm missing something silly here
Thanks for all your help apsillers, I have managed to sort this now. Cheers A
0

The answer to this is to use the provided success functionality in the jQuery plugin rather than what I did above. So

Event handler:

$('#formSEIncome').ajaxForm(options)

Options variable:

var options = { 
        target:        this  // target element to be updated with server response 
        ,success:       showResponse          
}; 

Success function:

function showResponse(responseText, statusText, xhr, $form)  { 
$form.parents('.trm_expense_cats').find('.itemlist').append('<li>Test</li>');
    alert('ok');
}

As such the use of this in the options.success object solved the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.