jquery: How to deal with 'this' in ajax callbacks

Posted by Svish on Stack Overflow See other posts from Stack Overflow or by Svish
Published on 2010-05-24T08:11:04Z Indexed on 2010/05/24 8:21 UTC
Read the original article Hit count: 185

Filed under:
|
|
|
|

I currently have code similar to this for a form:

$('#some-form')
    .submit(function()
    {
        // Make sure we are not already busy
        if($(this).data('busy'))
            return false;
        $(this).data('busy', true);

        // Do post
        $.post("some/url", $(this).serialize(), function(data)
        {
            if(data.success) // Success is a boolean I set in the result on the server
            {
                // Deal with data
            }
            else
            {
                // Display error
            }

            $('#some-form')
                .removeData('busy');
        });
        return false;
    });

My issue is that I would like to somehow remove the need for knowing the form id in the post callback. In the end where I remove the busy data from the form, I'd like to somehow not have that hard coded. Is there any way I can do this? Is there a way I can hand whatever is in this to the post callback function? Since I know the id right now, I can get around it by doing what I have done, but I'd like to know how to not be dependant on knowing the id, since often I don't have an id. (For example if I have a link in each row in a table and all the rows have the same click handler.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about best-practices