jQuery Form Plugin - How To Change 'target' (for response) Value

Christopher Cooper picture Christopher Cooper · May 13, 2009 · Viewed 9.1k times · Source

This is in reference to jQuery 1.3 and jQuery Form Plug 2.25.

Hopefully this is a cakewalk for even an amateur, but I am clueless.

var x;
$('div#response').fadeOut(300,function()
{
   // do something
   x = this;
}
$('#myForm').ajaxForm({ 
    target: x,
    success: function() 
      { 
        // do something
      }
});

What I'd like to do is define the target value as a variable I have pre-defined; we'll say it is "x". This is shown in the example above but the line "target: x," of course fails. How can I do this?

Additionally: I am aware that x = div#response in this example, but in the real world problem I am working on, I don't have a solid definition of x. I know in this example I could just change "target: x," to "target: div#response," and it'd work but this example is merely for argument. I need target to equal x. How do I do that?

Answer

Steve Willcock picture Steve Willcock · May 13, 2009

Assuming x is a jquery wrapped set, something like this should work:

$('#myForm').ajaxForm({
  success: function(responseText, statusText) {
    x.html(responseText);
  }
});