jQuery ajaxForm success: access the form

10k views Asked by At

I'm using jQuery Form plugin (https://github.com/malsup/form):

$("#dlgTask form").ajaxForm({
    success: function(res) {
        var form = this;
    }
});

In success function, form variable seems an object other than the form itself. How to access the form, so I can avoid using $("#dlgTask form") again for reusability? Thanks.

1

There are 1 answers

1
Lynn Adrianna On BEST ANSWER

The success' callback function has a 4th argument for your jQuery-wrapped form element.

$("#dlgTask form").ajaxForm({
    success: function(res, status, xhr, form) {
        // ...
    }
});

You can also simply store your form element before using ajaxForm()

var form = $("#dlgTask form");
form.ajaxForm({
    success: function(res) {
        // ... 
    }
});